help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] basename for n levels


From: Dennis Williamson
Subject: Re: [Help-bash] basename for n levels
Date: Wed, 10 Jun 2015 01:30:15 -0500

On Tue, Jun 9, 2015 at 11:32 PM, Peng Yu <address@hidden> wrote:

> Hi,
>
> The following thread shows for n = 2. Does anybody know what might be
> the most efficient and robust way to extract last n levels in bash?
>
>
> http://stackoverflow.com/questions/8223170/bash-extracting-last-two-dirs-for-a-pathname
>
> --
> Regards,
> Peng
>
>
You can iterate over the variable using basename and dirname or ${var%/*}
and ${var##*/}

or you can use read and an array

IFS=/ read -a parts <<< "$var"

and iterate over the array.

You can also put some of those pieces back together:

oIFS=$IFS
IFS=/ new="${parts[*]:2:3}"
IFS=$oIFS

-- 
Visit serverfault.com to get your system administration questions answered.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]