Reputation: 53
I am trying to write a bash script. I am not sure why in my script:
ls {*.xml,*.txt}
works okay, but
name="{*.xml,*.txt}"
ls $name
doesn't work. I get
ls: cannot access {*.xml,*.txt}: No such file or directory
Upvotes: 5
Views: 2678
Reputation: 6517
The reason your expansion doesn't work is that brace expansion is performed before variable expansion, see Shell expansions in the manual.
I'm not sure what it is you're trying to do, but if you want to store a list of file names, use an array:
files=( {*.txt,*.xml} ) # these two are the same
files=(*.txt *.xml)
ls -l "${files[@]}" # give them to a command
for file in "${files[@]}" ; do # or loop over them
dosomething "$file"
done
"${array[@]}"
expands to all elements of the array, as separate words. (remember the quotes!)
Upvotes: 1
Reputation: 42999
The expression
ls {*.xml,*.txt}
results in Brace expansion and shell passes the expansion (if any) to ls
as arguments. Setting shopt -s nullglob
makes this expression evaluate to nothing when there are no matching files.
Double quoting the string suppresses the expansion and shell stores the literal contents in your variable name
(not sure if that is what you wanted). When you invoke ls
with $name
as the argument, shell does the variable expansion but no brace expansion is done.
As @Cyrus has mentioned, eval ls $name
will force brace expansion and you get the same result as that of ls {\*.xml,\*.txt}
.
Upvotes: 4