bug-bash
[Top][All Lists]
Advanced

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

Re: [bug] Home dir in PS1 not abbreviated to tilde


From: Eric Blake
Subject: Re: [bug] Home dir in PS1 not abbreviated to tilde
Date: Tue, 13 Mar 2012 09:27:45 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.1) Gecko/20120216 Thunderbird/10.0.1

On 03/13/2012 09:18 AM, Roman Rakus wrote:

>> as a workaround to your problem you could have something like this in
>> your bashrc
>> if shopt extglob &>/dev/null ; then
>>   HOME="${HOME/%+(\/)}"   # strip all trailing forward slashes
>> else
>>    while [ "${HOME}" != "${HOME%\/}" ] ; do
>>      HOME="${HOME%\/}"
>>    done
>> fi
>>
>> I think it should hide your problem.
>>
>>
>>
>>
> Is it all necessary?
> HOME="${HOME%\/}"

That only strips one trailing slash.  If you want to strip multiple
trailing slashes, then you have to go with something more complex; but
the above if/shopt/else/loop approach is overkill, compared to this
one-liner:

$ foo=/a/b///
$ echo ${foo%%/}
/a/b//
$ echo ${foo%${foo##*[^/]}}
/a/b

Be aware that both approaches will misbehave if HOME is a root directory
(/ or //), where you _don't_ want to strip trailing slashes.  So you
really want:

case $HOME in
  *[^/]* ) HOME=${HOME%${HOME##*[^/]}} ;;
esac

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