When I needed to run a command multiple times with a different argument I used this approach (without understanding it fully):
touch {a,b,c}
Which is equivalent of:
touch a
touch b
touch c
I think the same could be accomplished with the following loop:
for file in {a,b,c}; do touch $file; done
However I’ve stumbled across a case where this does not work:
pear channel-discover {"pear.phpunit.de","pear.symfony-project.com"}
I have a few questions:
- What is the name of what is happening in the first example and what exactly is happening?
- Is it better to use this approach for simple things rather than for-in loops?
- Why the pear command is not working like that? Should the command script implement some techniques to handle such arguments or is it the shell responsible for that?
That’s called Brace Expansion, which expands to a space-separated list of the given strings.
So
touch {a,b,c}would be equivalent toWhile
touch {a,b,c}xwould be equivalent to:You
pearcommand would essentially be run as:which may not be what you expected. If you want the command to be run once for each string, use a for loop (which answers your 2nd question), or use a combination of brace expansion and xargs.