Reputation: 809
i have a simple problem with bash's brace expansion:
#!/bin/bash
PICS="{x1,x2,x3}.jpg {y1,y2}.png"
for i in $PICS
do
echo $i
done
but the result is:
{x1,x2,x3}.jpg
{y1,y2}.png
But i want the result is: x1.jpg x2.jpg x3.jpg y1.png y2.png
what should i do ?
Upvotes: 1
Views: 181
Reputation: 6577
These are files which already exist? If yes, you probably want a (ext)glob. E.g.
printf '%s\n' [xy]+([[:digit:]]).@(jp|pn)g
Brace expansion in Bash is the first expansion step. It occurs mostly in unquoted contexts, though the exact rules are complex. You cannot store one in a string unless you eval the result later.
printf '%s\n' {x{1..3}.jp,y{1,2}.pn}g
These can be defined however you feel. See other answers for less obfuscated options.
You also need to quote your expansions.
Upvotes: 0
Reputation: 2622
Brace and wildcard expansion is performed for arguments when a command is evaluated. Change the first line to:
PICS=$(echo {x1,x2,x3}.jpg {y1,y2}.png)
Upvotes: 1
Reputation: 185106
The straightforward way is
#!/bin/bash
for i in {x1,x2,x3}.jpg {y1,y2}.png; do
echo $i
done
Upvotes: 5
Reputation: 29578
Brace expansion is performed while parsing the line, and will not happen inside quotes.
Upvotes: 3