No such file or directory (ls) in conjunction with tilde expansion

こ雲淡風輕ζ 提交于 2020-01-04 02:11:47

问题


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:

  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.



来源:https://stackoverflow.com/questions/16862718/no-such-file-or-directory-ls-in-conjunction-with-tilde-expansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!