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.