0

I'm getting the below error while i executing the bash script.

#!/bin/bash

mynum=1

while [ $mynum -le 10 ]
do
    echo $mynum
    mynum= $(( $mynum + 1 ))
    sleep 0.5
done

error:

$./wl
1
./wl: line 8: 2: command not found
1
./wl: line 8: 2: command not found
1
./wl: line 8: 2: command not found
1
./wl: line 8: 2: command not found
1
./wl: line 8: 2: command not found
1
./wl: line 8: 2: command not found
1
./wl: line 8: 2: command not found
1
./wl: line 8: 2: command not found
  • 1
    The error is due to the space between mynum= and $(( $mynum + 1 )) which is causing the shell to try to execute 2 (the result of $(( $mynum + 1 ))) as a separate command – steeldriver Mar 11 '17 at 21:02
3

The correct way for incrementing variable is as follows (How to increment a variable in bash?)

#!/bin/bash
mynum=1
while [ $mynum -le 10 ]
do
    echo $mynum
    mynum=$((mynum+1))
    sleep 0.5
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.