Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
~$ 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?

share|improve this question
add comment

2 Answers

up vote 25 down vote accepted

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.

share|improve this answer
5  
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. –  Blacklight Shining Feb 10 at 5:44
add comment

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
share|improve this answer
6  
+1 for removing the redundant ls. –  Blacklight Shining Feb 10 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 Feb 10 at 15:18
    
@lngo: That's in the OP's post too –  staticx Feb 11 at 15:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.