bug-bash
[Top][All Lists]
Advanced

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

Re: invoke tilde expansion on quoted string


From: Eric Blake
Subject: Re: invoke tilde expansion on quoted string
Date: Sat, 12 Nov 2011 11:49:05 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110930 Thunderbird/7.0.1

On 11/12/2011 07:53 AM, Geir Hauge wrote:
> 2011/11/12 Chris F.A. Johnson <chris@cfajohnson.com>
> 
>> On Fri, 11 Nov 2011, Peng Yu wrote:
>>
>>> I'm wondering if I already have a string variable, is there a bash
>>> native to do tilde expansion on it.
>>>
>>> var='~/..'
>>> cd $var#how to change this line?
>>>
>>
>>  eval "cd $var"
>>
> 
> I'd avoid eval as that could potentially do more than just expand the
> tilde, depending on what other characters the var contains. I'd just
> replace the ~ with $HOME using parameter expansion.
> 
> cd "${var/#~\//$HOME/}"

Except that your proposed parameter expansion only works for plain ~.
It doesn't cover more useful tilde expansions, such as ~user/, which
does NOT expand to $HOME, but to "user"'s home directory.

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
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]