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. 
1
  • 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. Commented Jan 24, 2011 at 18:18

4 Answers 4

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 
Sign up to request clarification or add additional context in comments.

6 Comments

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).
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 when you encountered the error, did you have a $ sign at the beginning? It should be COUNTER=.. not $COUNTER=...
answer updated, assuming my hunch is correct.
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
|
6

Remove the spaces around the equals sign:

COUNTER=`expr $COUNTER + 1` 

Comments

3

Another way.

COUNTER=$(($COUNTER + 1)) 

1 Comment

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
2
for i in {0..4}; do echo "i will add this line to file mycreation" >> ./myfile done 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.