[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.
- [Help-bash] basename for n levels, Peng Yu, 2015/06/10
- Re: [Help-bash] basename for n levels,
Dennis Williamson <=
- Re: [Help-bash] basename for n levels, Peng Yu, 2015/06/10
- Re: [Help-bash] basename for n levels, Greg Wooledge, 2015/06/10
- Re: [Help-bash] basename for n levels, Peng Yu, 2015/06/10
- Re: [Help-bash] basename for n levels, Peter West, 2015/06/10
- Re: [Help-bash] basename for n levels, Greg Wooledge, 2015/06/10
- Re: [Help-bash] basename for n levels, Stephane Chazelas, 2015/06/10
Re: [Help-bash] basename for n levels, Eric Blake, 2015/06/10