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: Eric Blake
Subject: Re: [Help-bash] basename for n levels
Date: Wed, 10 Jun 2015 06:34:48 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

On 06/10/2015 06:29 AM, Eric Blake wrote:

>> http://stackoverflow.com/questions/8223170/bash-extracting-last-two-dirs-for-a-pathname
> 
> Please try harder to make your questions self-contained.  Making people
> chase down a URL is not as nice as directly pasting the code in question:

>>
>> DIRNAME = /a/b/c/d/e
>>
>> I'd like:
>>
>> DIRNAME2 = d/e
> 

Also, be aware that the constructs listed on that site are NOT robust to
all pathnames:

> DIRNAME2=$(basename $(dirname $DIRNAME))/$(basename $DIRNAME)

is improperly quoted (and therefore mishandles directory names
containing space), is not properly using -- to denote end of options
(and therefore mishandles a directory name that starts with -), and is
using command substitution on data that might end in a significant
newline (and therefore mishandles a directory name that ends in
newline).  Furthermore, it mishandles the case when $DIRNAME is // on
platforms where // is distinct from / (where the correct answer should
be "//", not "/////").

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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