~$ 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?

2 Answers 2


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
  • 7
    +1 for removing the redundant ls. Commented Feb 10, 2014 at 5:41
  • 3
    Nitpicking: You can run ls -l "*.txt". Or at least I can. I do it not often, but when I do, it usually tells me that it couldn't find *.txt ...
    – Ingo
    Commented Feb 10, 2014 at 15:18
  • @lngo: That's in the OP's post too Commented Feb 11, 2014 at 15:52

Remove the quotes around *.txt and it should work. With quotes shell will look for the literal filename *.txt. To explore/experiment, try creating a file with name *.txt as touch '*.txt' and repeat the command.

  • 6
    Note that double quotes (at least in bash and similar shells like zsh) are not the same as single quotes! For example, parameter expansion ($foo) is done inside double quotes, but not single quotes. Commented Feb 10, 2014 at 5:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .