3

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:

  1. Assign the string (foo{1..5}) representing an expansion to variable FOOS
  2. substitute $FOOS, so I've basically got for i in 'echo foo{1..5}' do (looks like I can't escape back-ticks here)
  3. execute echo, so I now have for i in foo1 foo2 foo3 foo4 foo5 do
  4. execute for, printing bar five times to the console output
  5. 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
2
  • Your first line doesn't work. $ FOOS = foo{1..5} FOOS: command not found. Please fix and provide the script exactly as is. Commented Oct 27, 2015 at 18:32
  • 1
    paste your script onto shellcheck.net and click the arrow. Commented Oct 27, 2015 at 18:49

1 Answer 1

3

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
2
  • 1
    Just use FOO=( {1..5} )。 Commented Oct 27, 2015 at 21:04
  • What am I doing wrong? v=$( echo {1,20{0..3}} ); echo $v yields 1 200 201 202 203 yet v=( {1,20{0..3}} ); echo $v yields only 1. Answer: Use echo ${v[*]}! Commented Nov 9, 2019 at 9:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.