10

I am trying to increment a variable in a bash script and it is not working. Here is my code:

#! /bin/bash

COUNTER=0
while [ $COUNTER -lt 5 ]
do
    echo "i will add this line to file mycreation">>./myfile
    COUNTER = `expr $COUNTER + 1`
done

The quotes around the COUNTER assignment are backticks.

I tried replacing COUNTER with $COUNTER like this:

$COUNTER = `expr $COUNTER + 1`

But that did not solve the problem and gave me the following error:

line7: 0: command not found. 
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
Marin
  • 12,531
  • 17
  • 56
  • 80
  • 2
    The command 0 not found error appears when you use `$COUNTER` on the LHS of the assignment - `$COUNTER` expands to 0 and the shell then tries to execute that command. – Jonathan Leffler Jan 24 '11 at 18:18

4 Answers4

34

As @Cory rightly pointed out, there should not be spaces around the equal sign or else bash will confuse COUNTER for a command.

COUNTER=$(expr $COUNTER + 1)

going off-topic ...

That said, you could avoid having bash fork a subprocess by using the following alternatives:

In fact, your while loop can be written as:

for ((COUNTER=0; COUNTER <= 5 ; COUNTER++))
do
    echo "i will add this line to file mycreation">>./myfile
done

Breaking down the error message

When you were met with the error:

line 7:   0:    command not found.
'-----'  '--'  '------------------'
   |       |                 |
location   |            Description of error.
          culprit 

my guess is what you had on line 7 was

$COUNTER = `expr $COUNTER + 1`
--------   --------------------
    |                 |
Evaluated to 0        |
                  Evaluated to 1

What bash ends up see is 0 = 1 and since bash statements are generally in the form command arg1 arg1 ..., bash interprets it as run the command 0 with arguments = 1. Thus the error message : 0: command not found.

When you removed the spaces around the equal sign, what bash ends up interpreting is:

0=1

which means run command 0=1 with no arguments, hence the error 0=1: command not found.

Variable assignments should be in the form VAR_NAME=VALUE (without the $), so the syntax you should be using is:

COUNTER=`expr $COUNTER + 1` # or any of the variants above

which bash evaluates and eventually interpret as:

COUNTER=2
Community
  • 1
  • 1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • Just be sure to shebang with `/bin/bash` if using bashisms, I think some of them actually turn off if bash is invoked as `/bin/sh` (POSIX) or if /bin/sh doesn't point to bash at all (dash on some systems). – Tim Post Jan 24 '11 at 18:29
  • Hello I removed the spaces around it however all it did is it's now giving me line:7 0=1 command not found. why did it change from 0:0 to 0:1 baffles me. Maybe i shouldn't be using expr at all? I don't understand why it's listed as useful command. Thanks – Marin Jan 24 '11 at 20:01
  • @Marin when you encountered the error, did you have a $ sign at the beginning? It should be COUNTER=.. not $COUNTER=... – Shawn Chin Jan 24 '11 at 20:14
  • answer updated, assuming my hunch is correct. – Shawn Chin Jan 24 '11 at 20:44
  • thanks Shawn! It worked, for some reason the spaces did the trick but I still don't get it why it's so sensitive? Thanks I guess I should just stick to AWK – Marin Jan 25 '11 at 17:14
  • +++++1 Thanks for explaining "let", "(( COUNT++ ))" and "for (( ))" =) – Viet Feb 16 '12 at 06:40
6

Remove the spaces around the equals sign:

COUNTER=`expr $COUNTER + 1`
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
3

Another way.

COUNTER=$(($COUNTER + 1))
webbi
  • 841
  • 7
  • 14
  • 3
    not well documented, but you don't have to dereference the variables inside the arithmetic expression: `COUNTER=$((COUNTER+1))` or `((COUNTER++))` as has been mentioned – glenn jackman Jan 24 '11 at 19:37
2
for i in {0..4}; do 
    echo "i will add this line to file mycreation" >> ./myfile
done
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60