PDA

View Full Version : bash: test -f doesn't accept quoted tilde from variable



trigone
October 22nd, 2015, 05:10 PM
hi,

i observed the following bash output:


>>> a=~/foo; [ -f $a ]; echo $?
0
>>> a="~/foo"; [ -f $a ]; echo $?
1
>>> a=~/foo; [ -f "$a" ]; echo $?
0
>>> a="~/foo"; [ -f "$a" ]; echo $?
1

my question is: in the case of files with whitespace in the name, is there a (simple) way to put its path in a variable, using the tilde, and making tests, readlink, etc, accept them as actual paths, and not, apparently, items inside /current/working/dir/~/. of course i suppose that individual escaping of whitespace with counterslash could work, but i wonder if there's an other way.

thanks in advance :)

Lars Noodén
October 22nd, 2015, 05:48 PM
The a=~/foo gets expanded by the shell before it is assigned to the variable , a="~/foo" doesn't. The latter means to take the string literally. Try adding an echo $a; to each line and see.

trigone
October 22nd, 2015, 05:56 PM
yes, i thought so; if you want, my question is, how to (easily) keep getting the effects of the interpretation of the shell of ~, even with a file with a name containing whitespace.

Lars Noodén
October 22nd, 2015, 06:00 PM
The way you mentioned above should work: use backslashes to escape the spaces.

Vaphell
October 23rd, 2015, 05:16 PM
you can also leave the tilde outside the quotes for it to work, but the best solution imo would be to stop depending on shell unrolling the ~ shortcut and use $HOME that always works instead.

trigone
October 25th, 2015, 05:30 PM
ah yes, $HOME is a great idea, much more flexible, thanks!