Using command line argument range in bash for loop prints brackets containing the arguments
Asked Answered
B

7

77

It's probably a lame question. But I am getting 3 arguments from command line [ bash script ]. Then I am trying to use these in a for loop.

for i in {$1..$2}
    do action1
done

This doesn't seem to work though and if $1 is "0" and $2 is 2 it prints {0..2}' and calls action1` only once. I referred to various examples and this appears to be the correct usage. Can someone please tell me what needs to be fixed here?

Borowski answered 17/4, 2011 at 2:45 Comment(0)
S
82

How about:

for i in $(eval echo {$1..$2}); do echo $i; done
Scarlettscarp answered 17/4, 2011 at 3:5 Comment(2)
I would recommend against using eval and consider splicing up the arguments as suggested in Vijayender's answer.Spelt
I think the 'better' solution is as mentioned below, "for i in seq $1 $2; do echo $i; done". eval with arbitrary arguments seems 'dangerous' to me.Doerr
G
87

You can slice the input using ${@:3} or ${@:3:8} and then loop over it

For eg., to print arguments starting from 3

for i in ${@:3} ; do echo $i; done

or to print 8 arguments starting from 3 (so, arguments 3 through 10)

for i in ${@:3:8} ; do echo $i; done
Gongorism answered 14/10, 2011 at 2:47 Comment(0)
S
82

How about:

for i in $(eval echo {$1..$2}); do echo $i; done
Scarlettscarp answered 17/4, 2011 at 3:5 Comment(2)
I would recommend against using eval and consider splicing up the arguments as suggested in Vijayender's answer.Spelt
I think the 'better' solution is as mentioned below, "for i in seq $1 $2; do echo $i; done". eval with arbitrary arguments seems 'dangerous' to me.Doerr
L
25

Use the $@ variable?

for i in $@
do
    echo $i
done

If you just want to use 1st and 2nd argument , just

for i in $1 $2 

If your $1 and $2 are integers and you want to create a range, use the C for loop syntax (bash)

for ((i=$1;i<=$2;i++))
do
...
done
Lugubrious answered 17/4, 2011 at 3:5 Comment(1)
You might want to only include the second part of the answer - It's better than using eval and fits the question.Armilla
R
15

I had a similar problem. I think the issue is with dereferencing $1 within the braces '{}'. The following alternative worked for me ..

#!/bin/bash
for ((i=$1;i<=$2;i++))
do
   ...
done

Hope that helps.

Rosemaryrosemond answered 22/12, 2012 at 17:5 Comment(1)
+1 IMO This is cleaner than having to eval as in the accepted answer, but the accepted answer is more specific to the OPAbridge
S
5
for i in `seq $1 $2`; do echo $i; done
Shelbashelbi answered 4/3, 2021 at 23:18 Comment(0)
A
0
#/bin/bash
for i
do
  echo Value: $i
done

This will loop over all arguments given to the script file. Note, no "do" or anything else after the loop variable i.

Amary answered 4/9, 2013 at 8:33 Comment(0)
P
0

I recommend this: In loop we handle only $1, and do shift N times (if arguments will be need next, then better save it, because shift removing argument):

for a in 1 2 3
do
echo $1
shift
done

Example: ./test.sh "abra k" "b 2" "c 14"

abra k
b 2
c 14
Pustule answered 17/7, 2021 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.