How to convert an input parameter to integer value in a for loop in bash?
Solution 1
You can do this two ways:
With ksh93-compatible shells (ksh93, zsh, bash):
for (( i=1;i<=$2;i++ ))
do
echo "Welcome $i times"
done
Here we set i to 1 and loop, incrementing it until it is less than or equal to $2, outputting:
Welcome 1 times
Welcome 2 times
With POSIX shells on GNU systems:
for i in $(seq "$2")
do
echo "Welcome $i times"
done
The seq
command (GNU specific) will output numbers from 1 to the number specified in $2 on separate lines. Assuming you've not modified $IFS
(which by default contains the line delimiter character), the command substitution will split that into as many elements for for
to loop on.
Solution 2
In Bash this construct that you're trying to use: {1..$(($2))}
or {1..$2}
is the brace expansion. The reason why it doesn't work for you is that in the order of shell expansions the brace expansion is the first one. So when this expansion takes place $2
is still only $2
, not 5
as you'd like. The brace expansion doesn't work then. It simply produces 1..$2
, and then when it's time to expand $2
, the shell does so, and you're left with this one word:
{1..5}
The braces are still there because they have not been consumed by the brace expansion.
As for solutions, see Raman Sailopal's answer.
Related videos on Youtube
Assa Yeroslaviz
Updated on September 18, 2022Comments
-
Assa Yeroslaviz almost 2 years
in my bash script I try to use a number as an input variable
for
a for loop I run the script as./script.sh InputFolder/ Number_of_iterations
the script should work inside the given folder and run a for loop as many times as the
Number_of_iterations
variable is set to. But somehow I can't set the variable as an integer.this is an example of my loop in the script:
for i in {1..$(($2))} do echo "Welcome $i times" done
I have tried already the double brackets
$(($...))
option as well as the double quotations"..."
, but the output I keep getting isWelcome {1..5} times
which make me think, this is not an integer. I would appreciate any help in reading the input parameter as an integer into the script.
-
Mikael Kjær over 6 yearsIf you insist on the {1..$ construct you can use something similar to this:
for i in $(eval echo "{1..$2}"); do
-
-
Kusalananda over 6 yearsThis depends on the shell. In
ksh93
,{1..$2}
would work without issues. -
Kusalananda over 6 years... and that's ok since brace expansions are a non-standard extension, so shells can choose to do them in any way they see fit.
-
Kusalananda over 6 yearsWith regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
-
Stéphane Chazelas over 6 years@Kusalananda, also in
zsh
(where the syntax comes from) andyash
.bash
is the only shell (with support for{x..y}
) where that doesn't work AFAIK. -
Admin over 6 years@Kusalananda Is it a reasonable question to ask?
-
Stéphane Chazelas over 6 years@tomasz, it's been asked many times before. It's a FAQ here
-
Kusalananda over 6 years@StéphaneChazelas Also shells like
pdksh
that has brace expansion but only supports alternation and not sequences. But that's nit-picking. -
Assa Yeroslaviz over 6 yearsthanks, this has solved my problem. I have used the second option.
-
Samuel over 3 yearsfirst one is not working in Ubuntu18.06