How to convert an input parameter to integer value in a for loop in bash? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1













This question already has an answer here:



  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers



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 is



Welcome 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.







share|improve this question














marked as duplicate by ilkkachu, Kusalananda, Rui F Ribeiro, dhag, jayhendren Jan 22 at 20:49


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.














  • If you insist on the {1..$ construct you can use something similar to this: for i in $(eval echo "1..$2"); do
    – Mikael Kjær
    Jan 22 at 10:29














up vote
1
down vote

favorite
1













This question already has an answer here:



  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers



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 is



Welcome 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.







share|improve this question














marked as duplicate by ilkkachu, Kusalananda, Rui F Ribeiro, dhag, jayhendren Jan 22 at 20:49


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.














  • If you insist on the {1..$ construct you can use something similar to this: for i in $(eval echo "1..$2"); do
    – Mikael Kjær
    Jan 22 at 10:29












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1






This question already has an answer here:



  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers



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 is



Welcome 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.







share|improve this question















This question already has an answer here:



  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers



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 is



Welcome 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.





This question already has an answer here:



  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers









share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 10:34









Kusalananda

103k13202319




103k13202319










asked Jan 22 at 9:51









Assa Yeroslaviz

102




102




marked as duplicate by ilkkachu, Kusalananda, Rui F Ribeiro, dhag, jayhendren Jan 22 at 20:49


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.






marked as duplicate by ilkkachu, Kusalananda, Rui F Ribeiro, dhag, jayhendren Jan 22 at 20:49


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.













  • If you insist on the {1..$ construct you can use something similar to this: for i in $(eval echo "1..$2"); do
    – Mikael Kjær
    Jan 22 at 10:29
















  • If you insist on the {1..$ construct you can use something similar to this: for i in $(eval echo "1..$2"); do
    – Mikael Kjær
    Jan 22 at 10:29















If you insist on the {1..$ construct you can use something similar to this: for i in $(eval echo "1..$2"); do
– Mikael Kjær
Jan 22 at 10:29




If you insist on the {1..$ construct you can use something similar to this: for i in $(eval echo "1..$2"); do
– Mikael Kjær
Jan 22 at 10:29










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










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



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.






share|improve this answer






















  • thanks, this has solved my problem. I have used the second option.
    – Assa Yeroslaviz
    Jan 22 at 10:59

















up vote
1
down vote













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.






share|improve this answer






















  • This depends on the shell. In ksh93, 1..$2 would work without issues.
    – Kusalananda
    Jan 22 at 10:29










  • @Kusalananda Good point, thanks.
    – Tomasz
    Jan 22 at 10:31










  • ... 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
    Jan 22 at 10:33










  • @Kusalananda Is the order of expansions the same in all shells?
    – Tomasz
    Jan 22 at 10:34










  • With regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
    – Kusalananda
    Jan 22 at 10:35


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










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



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.






share|improve this answer






















  • thanks, this has solved my problem. I have used the second option.
    – Assa Yeroslaviz
    Jan 22 at 10:59














up vote
3
down vote



accepted










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



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.






share|improve this answer






















  • thanks, this has solved my problem. I have used the second option.
    – Assa Yeroslaviz
    Jan 22 at 10:59












up vote
3
down vote



accepted







up vote
3
down vote



accepted






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



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.






share|improve this answer














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



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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 22 at 10:35









Stéphane Chazelas

281k53518849




281k53518849










answered Jan 22 at 10:22









Raman Sailopal

1,18117




1,18117











  • thanks, this has solved my problem. I have used the second option.
    – Assa Yeroslaviz
    Jan 22 at 10:59
















  • thanks, this has solved my problem. I have used the second option.
    – Assa Yeroslaviz
    Jan 22 at 10:59















thanks, this has solved my problem. I have used the second option.
– Assa Yeroslaviz
Jan 22 at 10:59




thanks, this has solved my problem. I have used the second option.
– Assa Yeroslaviz
Jan 22 at 10:59












up vote
1
down vote













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.






share|improve this answer






















  • This depends on the shell. In ksh93, 1..$2 would work without issues.
    – Kusalananda
    Jan 22 at 10:29










  • @Kusalananda Good point, thanks.
    – Tomasz
    Jan 22 at 10:31










  • ... 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
    Jan 22 at 10:33










  • @Kusalananda Is the order of expansions the same in all shells?
    – Tomasz
    Jan 22 at 10:34










  • With regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
    – Kusalananda
    Jan 22 at 10:35















up vote
1
down vote













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.






share|improve this answer






















  • This depends on the shell. In ksh93, 1..$2 would work without issues.
    – Kusalananda
    Jan 22 at 10:29










  • @Kusalananda Good point, thanks.
    – Tomasz
    Jan 22 at 10:31










  • ... 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
    Jan 22 at 10:33










  • @Kusalananda Is the order of expansions the same in all shells?
    – Tomasz
    Jan 22 at 10:34










  • With regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
    – Kusalananda
    Jan 22 at 10:35













up vote
1
down vote










up vote
1
down vote









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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 22 at 10:30

























answered Jan 22 at 10:27









Tomasz

8,04052560




8,04052560











  • This depends on the shell. In ksh93, 1..$2 would work without issues.
    – Kusalananda
    Jan 22 at 10:29










  • @Kusalananda Good point, thanks.
    – Tomasz
    Jan 22 at 10:31










  • ... 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
    Jan 22 at 10:33










  • @Kusalananda Is the order of expansions the same in all shells?
    – Tomasz
    Jan 22 at 10:34










  • With regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
    – Kusalananda
    Jan 22 at 10:35

















  • This depends on the shell. In ksh93, 1..$2 would work without issues.
    – Kusalananda
    Jan 22 at 10:29










  • @Kusalananda Good point, thanks.
    – Tomasz
    Jan 22 at 10:31










  • ... 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
    Jan 22 at 10:33










  • @Kusalananda Is the order of expansions the same in all shells?
    – Tomasz
    Jan 22 at 10:34










  • With regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
    – Kusalananda
    Jan 22 at 10:35
















This depends on the shell. In ksh93, 1..$2 would work without issues.
– Kusalananda
Jan 22 at 10:29




This depends on the shell. In ksh93, 1..$2 would work without issues.
– Kusalananda
Jan 22 at 10:29












@Kusalananda Good point, thanks.
– Tomasz
Jan 22 at 10:31




@Kusalananda Good point, thanks.
– Tomasz
Jan 22 at 10:31












... 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
Jan 22 at 10:33




... 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
Jan 22 at 10:33












@Kusalananda Is the order of expansions the same in all shells?
– Tomasz
Jan 22 at 10:34




@Kusalananda Is the order of expansions the same in all shells?
– Tomasz
Jan 22 at 10:34












With regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
– Kusalananda
Jan 22 at 10:35





With regards to the standard syntax and grammar, I would think so. But I have nothing backing that up.
– Kusalananda
Jan 22 at 10:35



Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?