~$ echo $(ls)
arguments.txt cecho.sh Desktop Documents Downloads example.txt Music Pictures Public
~$ echo $(ls "*.txt")
ls: cannot access *.txt: No such file or directory
Run Code Online (Sandbox Code Playgroud)
为什么第二个子shell命令失败?
小智 30
就像你不能ls "*.txt"
在普通 shell 中运行一样,你也不能在子 shell 中运行它。当您加上*.txt
引号时,您ls
搜索了一个名为*.txt
when的文字文件,而您应该这样做:
$ echo $(ls *.txt) # => file.txt otherfile.txt
Run Code Online (Sandbox Code Playgroud)
一个更好的方法是根本不使用ls
。
$ echo *.txt # => file.txt otherfile.txt
Run Code Online (Sandbox Code Playgroud)
mkc*_*mkc 27
删除周围的引号*.txt
,它应该可以工作。带引号的 shell 将查找文字文件名*.txt
。要探索/实验,请尝试创建一个名为*.txt
as的文件touch '*.txt'
并重复该命令。