Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

The {#..#} generate a sequence of numbers or chars, similarly to range() in Python. If I execute the command echo {1..5} in the command line I have:

1 2 3 4 5

Instead the following bash script doesn't work as expected.

for i in {1..3};
do
    echo "Iteration $i"
done

will print:

Iteration {1..3}

Why?
*I could use seq but I read it's outdate (http://www.cyberciti.biz/faq/bash-for-loop/)

share|improve this question
    
This error occur if the script is executed by the "sh" command (try using "bash"). –  Shake0 Jul 22 '12 at 3:39
add comment

1 Answer 1

up vote 4 down vote accepted

I've tried your code (copy paste into a terminal) and it produced a different result from what you posted:

for i in {1..3};
do
    echo "Iteration $i"
done
#---OUTPUT BELOW---
Iteration 1
Iteration 2
Iteration 3

Im using Linux Mint 12 Lisa (derived from Ubuntu) with bash version 4.2.10(1)-release

share|improve this answer
2  
You are right! I thought sh and bash were the same thing.. –  Shake0 Jul 22 '12 at 3:36
    
no, bash and sh are quite different... bash is more capable than sh, it was made on the shoulders of sh :) although they both have a good degree of compatibility, they also have its differences –  zipizap Dec 12 '13 at 14:13
    
note that you can do nested lists like for var in {{i..j},{l..m}};do echo $var;done if you need irregular sequence such as 1 2 3 7 8 9 –  brown.2179 May 9 at 15:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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