How to convert an input parameter to integer value in a for loop in bash? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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.
bash shell-script variable for
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.
add a comment |Â
up vote
1
down vote
favorite
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.
bash shell-script variable for
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
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
bash shell-script variable for
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
bash shell-script variable for
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
add a comment |Â
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
add a comment |Â
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.
thanks, this has solved my problem. I have used the second option.
â Assa Yeroslaviz
Jan 22 at 10:59
add a comment |Â
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.
This depends on the shell. Inksh93
,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
 |Â
show 4 more comments
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.
thanks, this has solved my problem. I have used the second option.
â Assa Yeroslaviz
Jan 22 at 10:59
add a comment |Â
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.
thanks, this has solved my problem. I have used the second option.
â Assa Yeroslaviz
Jan 22 at 10:59
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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.
This depends on the shell. Inksh93
,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
 |Â
show 4 more comments
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.
This depends on the shell. Inksh93
,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
 |Â
show 4 more comments
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.
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.
edited Jan 22 at 10:30
answered Jan 22 at 10:27
Tomasz
8,04052560
8,04052560
This depends on the shell. Inksh93
,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
 |Â
show 4 more comments
This depends on the shell. Inksh93
,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
 |Â
show 4 more comments
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