I am trying to print odd numbers from 1 - 99, using the script below.
start=1
while [[ $start -le 100 ]]
do
echo $start
start=start+2
done
but instead of getting numbers my output looks like
1
1+2
1+2+2
1+2+2+2
1+2+2+2+2
1+2+2+2+2+2
1+2+2+2+2+2+2
1+2+2+2+2+2+2+2
What did I miss here?
You have to use Aritmetic Expansion:
Arithmetic expansion provides a powerful tool for performing (integer) arithmetic operations in scripts.
Example:
start=$((start + 2))
The cleanest code I can do to print odd numbers is:
start=1
while [[ $start -le 100 ]]
do
echo $((start += 2))
done
Jest wrap the start+2 like below
start=$((start+2))
Here is some more details
The link is from This Answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With