20

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.

0

6 Answers 6

30

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<=number;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 -v 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.

6
  • 1
    I used seq 1 $number and it is working in bash.
    – Vombat
    Commented Aug 26, 2013 at 12:15
  • 12
    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?
    – replay
    Commented Aug 26, 2013 at 14:42
  • 1
    Can you elaborate on the 'avoid loops in shells' part please, if you have any example? I use for all the time. for i in ls... do something for that name.
    – 0fnt
    Commented Sep 13, 2014 at 16:19
  • 9
    Why do you recommend avoiding loops in shells? There are common cases of loop abuse, such as using while read instead of sed/awk/…, or looping over the output of find instead of using -exec. But a blanket “avoid loops in shells” is going too far. Commented Apr 25, 2015 at 23:40
  • 2
    @Wildcard For example, to process a list of files obtained from wildcard patterns, or to iterate over array elements. The answer you link is about processing texts, and even then there are cases where a while read loop is perfectly fine. Commented Oct 24, 2015 at 23:05
8

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
1
  • or seq $number if you want to assign some integer to $number. Commented Apr 4, 2023 at 23:59
4

You can use the following:

for (( num=1; num <= 100; num++ ))
do
    echo $num
done
2

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
0
1

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

1

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
1
  • 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
    Commented Aug 26, 2013 at 13:17

Not the answer you're looking for? Browse other questions tagged .