Print numbers from 1-50
A shell is not a (good) programming language, it's (before all) a command line interpreter. Use a counting command if you want to count, not the echo
and [
commands in a loop.
For instance, GNU systems have the seq
command for that. Alternatives are awk
or bc
for instance:
seq 50
echo 'for (i=1; i<=50; i++) i' | bc
awk 'BEGIN {for (i=1; i<= 50; i++) print i}'
If you find yourself using a loop in shells, chances are you're going for the wrong approach.
On line 5:
Change $x=(($x + 1))
to x=$(($x + 1))
.
Instead of using an entire bash script, you can just use seq 1 50
.
If the case were x=$(($x + 2))
, you could use seq 1 2 50
, where 2 denotes step/increment.
Print numbers from 1-50
printf '%s\n' {1..50}
print numbers from 1-50 with step say 2 (bash 4+):
printf '%s\n' {1..50..2}