Reputation: 15856
I want to put path with globs in a variable in zsh and later use it in various commands, but can't figure out how to do that...
└➜ path_var="expansion_test/{loc,diff-loc}*"
But if I try to use the variable, I get an error:
┌~/temp
└➜ ls -l $path_var
ls: cannot access expansion_test/{loc,diff-loc}*: No such file or directory
┌~/temp
└➜ ls -l $~path_var
zsh: no matches found: expansion_test/{loc,diff-loc}*
This is no matches found error is really strange though:
┌~/temp
└➜ ls -l expansion_test/{loc,diff-loc}*
expansion_test/diff-loc:
total 0
-rw-r--r-- 1 maxims eng 0 Jul 24 11:10 some13
-rw-r--r-- 1 maxims eng 0 Jul 24 11:10 some5
-rw-r--r-- 1 maxims eng 0 Jul 24 11:10 somefile
-rw-r--r-- 1 maxims eng 0 Jul 24 11:10 something
expansion_test/loc1:
total 0
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile1
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile2
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile3
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile4
expansion_test/loc2:
total 0
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile10
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile5
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile6
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile7
-rw-r--r-- 1 maxims eng 0 Jul 24 11:09 testfile8
What am I doing wrong?
Upvotes: 3
Views: 682
Reputation: 4808
The reason why your expansion is working is that brace expansions don't expand in double-quotes. Also, to use the brace expansion and set it to a variable in one go, you'll need to wrap it in parentheses.
Try this:
$ path_var=("expansion_test/"{loc,diff-loc}*)
$ echo $path_var
expansion_test/diff-loc expansion_test/loc1 expansion_test/loc2
Upvotes: 5