Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When you use brace expansion in bash, something like

echo {a,b,c}

becomes

echo a b c

Is there a way to expand it to 3 separate commands, one for each expansion, instead?

So that:

echo {a,b,c}

would become

echo a
echo b
echo c
share|improve this question

3 Answers 3

up vote 10 down vote accepted

Is this just a "because I wanna know" question, or is there a real use case for it? We could go through some gymnastics to get it done:

$ eval echo\ {a,b,c}\;
a
b
c

But I'd hunt down anyone that was putting in these kinds of obfuscatory commands into our system scripts.

Why not go for clarity instead:

$ for X in {a,b,c}; do echo $X; done

You could even go whole-hog and put in a couple of newlines and indent it a bit so that you'd always be able to understand what you were trying to do.

share|improve this answer
2  
@peth - I'm assuming this is a simplified example. If it is indeed as simple as a list, then you're right. If it's something more like file-{r,b,g}{m,n,o}{x,y.z}.foo then it's better to use the short form than enumerating all 27 value. –  Mark Mann Jul 1 '11 at 4:15
    
@peth - I thought it was a good follow-up question. –  Mark Mann Jul 1 '11 at 4:34
    
i've wanted to know this for a long time, thanks ! :) –  Sirex Jul 1 '11 at 13:02
    
Well, the use case was programs that don't accept more than one file (for instance), so where the expansion wouldn't work. Therefore, it'd be nice to be easily able to just run the program multiple times. The for works for this, but it's still more typing :P –  houbysoft Jul 2 '11 at 3:51

Based on Mark Mann's selected answer, I was able to further derive this example, which works great:

$ eval echo\ category_{17,32,33}.properties\{,.bak\}\;
category_17.properties category_17.properties.bak
category_32.properties category_32.properties.bak
category_33.properties category_33.properties.bak

What that is showing, is when you are using multiple occurrences of brace expansion within a line, Mark's original example would have printed every variation individually. Instead, I wanted to use his answer to move/rename multiple files. To ensure that the output matched the format that mv normally expects (mv oldfilename newfilename), I escaped the second occurrence of brace expansion, so that it wouldn't be evaluated until after the initial eval command had executed.

As the above output appeared as expected, I was then able to run the following command:

$ eval mv\ category_{17,32,33}.properties\{,.bak\}\;
$ ls
category_17.properties.bak  category_32.properties.bak  category_33.properties.bak

Many thanks to Mark for his original answer. Please up-vote his answer if you like what his answer allowed me to do :-)

share|improve this answer
printf '%s\n'  echo{\ a,\ b,\ c}
share|improve this answer
    
eval echo{\ a\;,\ b\;,\ c\;} –  Brian Vandenberg Sep 7 '11 at 17:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.