On 11/12/2011 07:53 AM, Geir Hauge wrote: > 2011/11/12 Chris F.A. Johnson <ch...@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 ebl...@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature