[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] No tilde expansion in [[..]] when reading input with rea
Re: [Help-bash] No tilde expansion in [[..]] when reading input with read
Mon, 25 Feb 2013 08:01:01 +0800
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
$ echo $x
Obviously this doesn't account for cases like ~chris, for example, but it's a