Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to print list of numbers from 1 to 100 and I use a for loop like the following:

number=100
for num in {1..$number}
do
  echo $num
done

When I execute the command it only prints {1..100} and not the list of number from 1 to 100.

share|improve this question

marked as duplicate by jordanm, rahmu, slm, Anthon, Mat Aug 26 '13 at 17:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6 Answers 6

Yes, that's because brace-expansion occurs before parameter expansion. Either use another shell like zsh or ksh93 or use an alternative syntax:

Standard (POSIX) sh syntax

i=1
while [ "$i" -le "$number" ]; do
  echo "$i"
  i=$(($i + 1))
done

Ksh-style for ((...))

for ((i=1;i<100;i++)); do
  echo "$i"
done

use eval (not recommended)

eval '
  for i in {1..'"$number"'}; do
    echo "$i"
  done
'

use the GNU seq command on systems where it's available

unset IFS # restore IFS to default
for i in $(seq "$number"); do
  echo "$i"
done

(that one being less efficient as it forks and runs a new command and the shell has to reads its output from a pipe).

Avoid loops in shells.

Using loops in a shell script are often an indication that you're not doing it right.

Most probably, your code can be written some other way.

share|improve this answer
    
I used seq 1 $number and it is working in bash. –  coffeMug Aug 26 '13 at 12:15
3  
That's off-topic, but I have to ask anyway: What is the problem with loops in shells? Why is it bad to use them? –  mauro.stettler Aug 26 '13 at 14:42

The brace expansion only works for literal integers or single characters. It happens before variable expansion, so you cannot use variables in it.

share|improve this answer

Also there is a pre increment.

for (( int=1; int <= 100; ++int));
  do
    printf '%s ' $int
  done

Use printf to print the numbers in one row instead.

Another example to increment by 2

for (( int=1; int <= 100; int+=2));
  do
    printf '%s ' $int
  done
share|improve this answer
    
The first code block presents an alternative solution. However, the answer does not address the current question directly -- an explanation on why the original code failed would be desirable. –  Barun Aug 26 '13 at 13:17

You don't even need a for loop for this, just use the seq command:

$ seq 100

Example

Here's the first 10 numbers being printed out:

$ seq 100 | head -10
1
2
3
4
5
6
7
8
9
10
share|improve this answer

You can use the following:

for (( num=1; num <= 100; num++ ))
do
    echo $num
done
share|improve this answer

Another way to do it simply in Bash script (and that looks like what you were doing)

number=100
for num in $(seq 1 $number); do
    echo $num;
done
share|improve this answer
    
It should be echo $i –  Barun Aug 26 '13 at 13:56

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