[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] No tilde expansion in [[..]] when reading input with rea
From: |
Chris Down |
Subject: |
Re: [Help-bash] No tilde expansion in [[..]] when reading input with read |
Date: |
Mon, 25 Feb 2013 08:01:01 +0800 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Hi,
On 2013-02-25 00:03, Suvayu Ali wrote:
> I'm trying to read in a directory name with read, later when I test it
> with [[ -d $mydir ]] it fails if there is a tilde. I'm guessing tilde
> expansion is not happening in [[...]]. I have attached a test script
> which demonstrates my issue.
The reason it is not expanding is not because of anything in [[, it's because
it's not interpreted when you're reading it into the variable using `read'.
$ read x
~/
$ echo "$x"
~/
If you need to interpret the tilde, you should manually replace it (do not use
`eval' without thinking long and hard about it first, you can quite easily allow
arbitrary command execution). One way of doing this is to use bash's built-in
parameter expansion:
$ x=${x/\~/$HOME}
$ echo $x
/home/chris/
Obviously this doesn't account for cases like ~chris, for example, but it's a
start.
Best,
Chris