25

I am trying to pass the argument as max limit for the for loop like this:

#!/bin/bash

for i in {1..$1}
do
    echo $i
done

This however returns {1..2} when called with argument 2, instead of executing the script and giving me

1
2
Milo Wielondek
  • 4,164
  • 3
  • 33
  • 45

4 Answers4

43

Variable substitutions are not done inside of curly braces. You can use fixed numbers but not variables.

Brace Expansion

A sequence expression takes the form {x..y}, where x and y are either integers or single characters. ...

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.

A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma or a valid sequence expression. Any incorrectly formed brace expansion is left unchanged.

Try one of these alternatives:

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

# Not recommended with large sequences.
for i in $(seq 1 $1); do
    echo $i
done
Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    Your solution is correct, but your reasoning is incorrect. Brace expansion occurs before variable substitution, but `{1..$1}` is not a valid brace expansion. – Ignacio Vazquez-Abrams Jan 21 '11 at 22:20
  • Thanks, the first solution did it (the second solution is obsolete). You might want to change `2`s into `$1` to fit my original question. – Milo Wielondek Jan 21 '11 at 22:36
  • `ksh93` and `zsh` have a different order of expansion: `ksh93 -c 'n=3; for i in {1..$n}; do echo $i; done'` – Dennis Williamson Jan 22 '11 at 01:04
  • Out of curiosity, can you explain why `$(seq 1 $1)` isn't recommended for large sequences? I'm assuming `seq` has a significant performance degradation there, but an explanation might still be useful if some users assume it's to do with the general `for` loop syntax (i.e. making it clear that this isn't an `ARG_MAX` issue) – Reinstate Monica Please Aug 12 '14 at 08:19
7

This will cycle through all true arguments (a.k.a. "testo mesto" is one argument)

#cycle through all args
for (( i=1; i<=$1; i++ )); do
    eval arg=\$$i
    echo "$arg"
done

OR

#cycle through all args
for (( i=1; i<=$1; i++ )); do
    echo "${!i}"
done
lucascaro
  • 16,550
  • 4
  • 37
  • 47
Daniel K
  • 71
  • 1
  • 1
4

...or in the unlikely event that you really just want sequential numbers:

seq $1

:-)

Lasse
  • 686
  • 4
  • 9
3

As well as John Kugelman's solution, you can use eval like this:

x=10; for i in $(eval echo {1..$x}); do echo $i; done

Or, if $1 is 10, then:

set -- 10
for i in $(eval echo {1..$1})
do
    echo $i
done

You could also use some variants on:

set -- 1000
eval echo {1..$1} |
while read i
do
    echo $i
done

Or:

set -- 1000
while read i
do
     echo $i
done <(eval echo {1..$1})

That uses process substitution.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278