I've got the following script:
FOOS=foo{1..5}
for i in `echo $FOOS` do
echo bar
done
Now I (think I) get the idea that the brace expansion only works with literals - hence the echo - but all this seems to do is print just one bar
to the console. Why (is string/brace expansion not working)?
What I'd expect to happen is:
- Assign the string (
foo{1..5}
) representing an expansion to variableFOOS
- substitute
$FOOS
, so I've basically gotfor i in 'echo foo{1..5}' do
(looks like I can't escape back-ticks here) - execute echo, so I now have
for i in foo1 foo2 foo3 foo4 foo5 do
- execute for, printing
bar
five times to the console output - Not get just one
bar
printed on screen as the only output :-)
Rui F Ribeiro
57.9k28 gold badges154 silver badges237 bronze badges
asked Oct 27, 2015 at 18:27
1 Answer 1
1.
FOO=$(echo foo{1..5})
but better is to use an array
FOO=(foo{1..5})
2.
echo ${FOO[*]}
4.
for i in {1..5}
do
echo bar
done
or
for i in $(seq 5)
do
echo bar
done
answered Oct 27, 2015 at 19:14
-
1Just use FOO=( {1..5} )。Mingye Wang– Mingye Wang2015年10月27日 21:04:53 +00:00Commented Oct 27, 2015 at 21:04
-
What am I doing wrong?
v=$( echo {1,20{0..3}} ); echo $v
yields1 200 201 202 203
yetv=( {1,20{0..3}} ); echo $v
yields only1
. Answer: Useecho ${v[*]}
!Giszmo– Giszmo2019年11月09日 09:27:44 +00:00Commented Nov 9, 2019 at 9:27
You must log in to answer this question.
lang-bash
$ FOOS = foo{1..5} FOOS: command not found
. Please fix and provide the script exactly as is.