PDA

View Full Version : [SOLVED] [BASH] Brace expansion



ofnuts
September 10th, 2011, 09:15 PM
If I do:

echo {a,b}I get
a bIf I do:

x={a,b}
echo $xI get
{a,b}Besides the not-so-pretty
x=$(echo {a,b})Is there a more elegant way to set a variable from a brace expansion?

dave01945
September 10th, 2011, 09:51 PM
if you do


x={a,b}
echo $x

your output will be


{a,b}

as you already know

if you do


x={a,b}
eval echo $x

your output will be


a b

is this what you was looking for

ofnuts
September 10th, 2011, 10:02 PM
if you do


x={a,b}
echo $xyour output will be


{a,b}as you already know

if you do


x={a,b}
eval echo $xyour output will be


a bis this what you was looking for
I was looking for an eval-less solution... (and possibly, the rationale behind this restriction)

dave01945
September 10th, 2011, 10:15 PM
these pages should explain why eval is required


why use eval with variable expansion (http://fvue.nl/wiki/Bash:_Why_use_eval_with_variable_expansion%3F)

Bash shell brace expansion (semicrazy.wordpress.com/2009/09/09/fun-with-bash-shell-brace-expansion/)

ofnuts
September 10th, 2011, 11:55 PM
these pages should explain why eval is required


why use eval with variable expansion (http://fvue.nl/wiki/Bash:_Why_use_eval_with_variable_expansion%3F)

Bash shell brace expansion (http://semicrazy.wordpress.com/2009/09/09/fun-with-bash-shell-brace-expansion/)
I don't get it... the first page says that brace expansion occurs before about everything else.

so x={1..3} should initialize x to "1 2 3"

As to the other page it says:


echo values_{$one,$two}values_1 values_2
but
x=1
y=5
echo {$x..$y}
{1..5}
so this example must be working for the wrong reason.

dave01945
September 11th, 2011, 01:37 PM
echo values_{$one,$two}values_1 values_2
but
x=1
y=5
echo {$x..$y}
{1..5}


i think it is because the braces are expanded before the variables and because the braces contain variables there is nothing to expand the range until the variables have been expanded if we do


echo {1..5}

gives us
[

1 2 3 4 5

also if we use a for loop


for x in {a,b}
do
echo value$x
done

this gives the output


valuea
valueb

ofnuts
September 11th, 2011, 02:46 PM
i think it is because the braces are expanded before the variables and because the braces contain variables there is nothing to expand the range until the variables have been expandedWhat I don't get is the difference of behavior betwen the list syntax (comma) and the sequence one (two dots):


=>echo a{1,3}
a1 a3
=>echo a{1..3}
a1 a2 a3and


=>x=1
=>y=3
=>echo a{$x,$y}
a1 a3
=>echo a{$x..$y}
a{1..3}I would expect either full expansion for both


=>x=1
=>y=3
=>echo a{$x,$y}
a1 a3
=>echo a{$x..$y}
a1 a2 a3 or no expansion for either:


=>x=1
=>y=3
=>echo a{$x,$y}
a{1,3}
=>echo a{$x..$y}
a{1..3}

dave01945
September 11th, 2011, 03:28 PM
the way i see it is that with the example


a=1
b=5
echo {$a,$b}

1 5

there is no expansion because they are separate values but with


a=1
b=5
echo {$a..$b}

{1..5}

the braces need expanding to fill in the number 2 3 5 and because the braces expanded before the variables are expanded then it gives the output as above

sisco311
September 11th, 2011, 04:46 PM
http://wiki.bash-hackers.org/syntax/expansion/brace

^^ explains brace expansion very well

ofnuts
September 11th, 2011, 05:57 PM
http://wiki.bash-hackers.org/syntax/expansion/brace

^^ explains brace expansion very well

OK, got it. "{$x,$y}" is seen by brace expansion as a list of two strings, "$x" and "$y", so it's expanded to "$x $y" and the variable substitution that occurs later replaces $x and $y by their values, yielding "1 3". OTOH "{$x..$y}" is not seen as a range since $x and $y are just "$x" and "$y" so there is no expansion and it remains as "{$x..$y}" and when variable substitution occurs this yields "{1..3}".

Fun conclusion:


=>v1=value1
=>v2=value2
=>v3=value3
=>echo $v{1..3}
value1 value2 value3