Brace expansion of {1..$n} [duplicate] - bash

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 4 years ago.
I am trying to loop from 1 to n where n is from user input.
If I do:
read n
echo {1..$n}
I get this output for the input 5
{1..5}
How do I make it expand to
1 2 3 4 5

Keep it simple by trying to do it with a for loop as follows.
echo "enter number..."
read n
for((i=1;i<=n;i++)); do
echo "$i"
done
Or use seq with for loop as follows too.
echo "Enter number:"
read howmany
for i in $(seq 1 $howmany); do
echo "$i";
done
Curly braces don't support variables in bash, though eval could be used but it is evil and have loopholes, why so see this link carefully http://mywiki.wooledge.org/BashFAQ/048

Related

Why is bash behaving differently when it takes argument in braces? [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 1 year ago.
Input: range 6
function range {
echo {0..$1}
echo {0..6}
if [[ $1 =~ 6 ]]
then
echo "Equal"
fi
}
Output:
{0..6}
0 1 2 3 4 5 6
Why is the output different whereas $1 and 6 is equal?
The curly-braces get expanded before $-variables are expanded, and the curly-brace range syntax simply doesn't recognize anything other than a pair of numbers with dots in between.

Why two numbers not summarized in script? [duplicate]

This question already has answers here:
How do I echo a sum of a variable and a number?
(9 answers)
How can I do basic maths in bash?
(4 answers)
How do I print the result of a command with 'echo'?
(1 answer)
Closed 4 years ago.
I am newer in bash.
I try to create script that summarizetwo numbers:
Here is script:
echo "the result is:" expr $1+$2
Here how I call the script:
./scr3 50 98
And here is result that I get after the script executed:
the result is: expr 50+98
While I get the the string with two summarized numbers I expect to get the summarize of two numbers.
My question is why I don't get the result of summarize of the two numbers?
Why? Because echo prints its arguments, and you're passing expr as an argument.
A best-practice alternative would be:
echo "The result is: $(( $1 + $2 ))"
...though the a smaller change (albeit to very inefficient code; expr is an artifact of the 1970s, made irrelevant with the introduction of $(( )) in the 1992 POSIX sh standard, and should never be used in new development) is simply:
echo "The result is: $( expr "$1" + "$2" )"

How to loop an argument value in bash [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I want to loop over the value of an input argument, something like that
for i in {0..$1} do
echo $i
done
If i call my script: ./my.sh 2
I want
0
1
But i get
{0..2}
How can i do it ?
#!/usr/bin/env bash
last=$(("$1"-1))
for i in $(seq 0 "$last");
do echo "$i";
done

Calculating a loop size in bash script gives back different output than writing the size hard coded [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 5 years ago.
Is it possible to do something like this:
start=1
end=10
echo {$start..$end}
# Ouput: {1..10}
# Expected: 1 2 3 ... 10 (echo {1..10})
In bash, brace expansion happens before variable expansion, so this is not directly possible.
Instead, use an arithmetic for loop:
start=1
end=10
for ((i=start; i<=end; i++))
do
echo "i: $i"
done
OUTPUT
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
i: 10
You should consider using seq(1). You can also use eval:
eval echo {$start..$end}
And here is seq
seq -s' ' $start $end
You have to use eval:
eval echo {$start..$end}
If you don't have seq, you might want to stick with a plain for loop
for (( i=start; i<=end; i++ )); do printf "%d " $i; done; echo ""
I normally just do this:
echo `seq $start $end`
Are you positive it has be BASH? ZSH handles this the way you want. This won't work in BASH because brace expansion happens before any other expansion type, such as variable expansion. So you will need to use an alternative method.
Any particular reason you need to combine brace and variable expansion? Perhaps a different approach to your problem will obviate the need for this.
use -s
ex:
seq -s ' ' 1 10
output: 1 2 3 4 5 6 7 8 9 10

For loop running only once in Bash [duplicate]

This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 7 years ago.
I've just started shell scripting.
Here is my code:
read totalNumbers
for i in {1..$totalNumbers..1}
do
echo "Welcome $i times"
done
When the input is given as 100, I'm getting the output as
Welcome {1..100..1} times
But I need something like
Welcome 1 times
Welcome 2 times
Welcome 3 times
.
.
.
Welcome 100 times
You cannot use variable names inside {..} in shell.
You can use ((..)) for arithmetic in BASH:
for (( i=1; i <= $totalNumbers; i++ )); do
echo "Welcome $i times"
done
Even for (( i=1; i <= totalNumbers; i++ )) would work.
If you want to use
{1..$totalNumbers..1}
anyway, replace it by
$(eval echo {1..$totalNumbers..1})
$(...) runs a subshell.

Resources