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
azardin
  • 145
  • 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

1 Answers1

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
Future
  • 146