~$ 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
Why does the 2nd subshell command fails?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
In the same way that you can't run
ls "*.txt" in a normal shell, you can't run it in a subshell either. When you put
*.txt in quotes, you made
ls search for a literal file called
*.txt when instead you should be doing this:
$ echo $(ls *.txt) # => file.txt otherfile.txt
A better way to do this is to not use
ls at all.
$ echo *.txt # => file.txt otherfile.txt