问题
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!
回答1:
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:
- Tilde expansion
- parameter expansion
- command substitution
- arithmetic expansion
- Field splitting
- Pathname expansion
- Quote removal
See the POSIX Shell Specification on Word Expansions for the gory details.
来源:https://stackoverflow.com/questions/16862718/no-such-file-or-directory-ls-in-conjunction-with-tilde-expansion