Question

I am writing a simple bash script and wanted to display all the items in a a particular directory. I tried doing the following:

desktop="~/Desktop/testr/"
echo $desktop
echo `ls $desktop`

However I keep getting the output:

~/Desktop/testr/
ls: ~/Desktop/testr/: No such file or directory

But when I run ls from the terminal, I can see the items. I suspect that the problem is that the ~ is not getting expanded but I thought that the double quotes would have taken care of that.

Thanks for your help!

Was it helpful?

Solution

This is because within quoted strings there is no tilde expansion and tilde expansion comes before parameter substitution in the echo line.

The sequence of expansions is:

  1. Tilde expansion
  2. parameter expansion
  3. command substitution
  4. arithmetic expansion
  5. Field splitting
  6. Pathname expansion
  7. Quote removal

See the POSIX Shell Specification on Word Expansions for the gory details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top