2

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 :-)
  • Your first line doesn't work. $ FOOS = foo{1..5} FOOS: command not found. Please fix and provide the script exactly as is. – Mikel Oct 27 '15 at 18:32
  • 1
    paste your script onto shellcheck.net and click the arrow. – Arthur2e5 Oct 27 '15 at 18:49
2

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
  • 1
    Just use FOO=( {1..5} )。 – Arthur2e5 Oct 27 '15 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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