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: Peng Yu
Subject: Re: [Help-bash] basename for n levels
Date: Wed, 10 Jun 2015 07:54:59 -0500

On Wednesday, June 10, 2015, Dennis Williamson <address@hidden>
wrote:

>
>
> On Tue, Jun 9, 2015 at 11:32 PM, Peng Yu <address@hidden
> <javascript:_e(%7B%7D,'cvml','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"
>

This is not robust wrt multiple consecutive slashes.

>
> 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.
>


-- 
Regards,
Peng


reply via email to

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