Bash – How to perform command substitution before brace expansion

bashbrace-expansioncommand-substitution

I would like to perform command substitution before brace expansion, but couldn't:

$ ls {$(seq -s , 13 20)}.pdf
ls: cannot access {13,14,15,16,17,18,19,20}.pdf: No such file or directory

How can I do it?

Best Answer

You simply need to use the eval shell builtin:

$ eval ls {$(seq -s , 13 20)}.pdf

Where eval takes the arguments passed to it:

ls {$(seq -s , 13 20)}.pdf

and concatenates them together into a single command:

ls {13,14,15,16,17,18,19,20}.pdf

which is then read and executed by the shell.

$ eval ls {$(seq -s , 13 20)}.pdf
13.pdf  14.pdf  15.pdf  16.pdf  17.pdf  18.pdf  19.pdf  20.pdf
Related Question