3

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?

1 Answer 1

5

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works. I guess my main mistake is that I thought $~path_var would expand braces too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.