I am new to shell scripting and I am trying to learn while loop in an ubuntu shell script. Script.sh
#!/bin/bash
#Initializing two variables
#while loop
echo while loop
a=0
while [ $a -lt 10 ]
do
echo $a
a=$(a + 1)
done
Output is -
vscoder@vscoder-VirtualBox:~$ ./script.sh
while loop
0
./script.sh: line 10: a: command not found
./script.sh: line 7: [: -lt: unary operator expected
vscoder@vscoder-VirtualBox:~$
I have no idea what is the error. Somebody please help me with the issue.
a=$(( a + 1 )). Note you need to count upwards to meet the exit condition. A for loop would be a better construct here:for a in {0..9}; do ... doneorfor (( a = 0; a < 10; a++ )); do ... done.-lt(less than) and similar.<is being interpreted as a file redirection, with 10 as the filename.shellcheck.netis your friend here.<is a redirection (in[ ... ], not in[[ ... ]]). Quoting it makes it compare sort order.