Bash counter increment not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash counter increment not working
# 1  
Old 05-30-2014
Bash counter increment not working

Hi all,

I'm using Bash 4.3.8 on an Ubuntu system, and no matter what I try, incrementing a counter won't work. The simplest example would be something like this:

Code:
#!/bin/bash
myVar=0
myVar=$((myVar++))
echo myVar

The variable should be 1, but it's always 0. I've tried every increment method I know, including:

Code:
myVar=$((myVar++))
myVar=$(($myVar+1))
myVar=`expr myVar + 1`
myVar++

And nothing works. I'm pulling my hair out here, can anyone tell me what I'm missing?

Thanks!
Zel2008
# 2  
Old 05-30-2014
Code:
myVar=$((myVar+1))  #note that there is no $ sign in myVar 
myVar=`expr $myVar + 1` #note that there has to be $ sign in myVar

This User Gave Thanks to clx For This Post:
# 3  
Old 05-30-2014
Thanks clx,

That's almost got it -- it seems to work outside of a loop, but not inside a loop. If I have a setup like this:

Code:
#!/bin/bash
myVar=0
perl -ne 'print if s|blah||' test.xml | while read line
do
echo $line
myVar=$((myVar+1))
echo $myVar
done
echo $myVar

Why does myVar have the correct value inside the loop, and not outside? The output basically goes:

Code:
blah
1
blah
2
blah
3
0

Thanks,
Zel2008
# 4  
Old 05-30-2014
That's the variable scoping. The loop runs in subshell and hence they doesn't return the value back to the parent shell.

You can force everything to be executed in the current shell.


Code:
 | {
while read line
do
echo $line
myVar=$((myVar+1))
echo $myVar
done
echo $myVar
}

This User Gave Thanks to clx For This Post:
# 5  
Old 05-30-2014
The while loop, in this case runs in another subshell with its own scope of variables, which are created and destroy as the while loop starts and finishes.

Some more explanation
This User Gave Thanks to Aia For This Post:
# 6  
Old 05-30-2014
Thank you both, I didn't know that bash runs in subshells. I learn something new every day, that's the mark of a good Friday. Smilie Everything's working fine now.
# 7  
Old 05-30-2014
Code:
#!/bin/bash
var=0
echo $var
((var++))
echo $var

Whats the matter? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash menu item counter

We have a simple menu with prompt of menu numbers to user. It is still under construction. Is there a way to "count" the menu choices so the prompt maximum count can be changed dynamically? See attached TODO note in code read_options(){ local choice # the... (7 Replies)
Discussion started by: annacreek
7 Replies

2. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

3. Shell Programming and Scripting

Bash 4.0 increment variable

Hi there everyone! This is my first post so be gentle. I have a small bash script that is extracting 3 line every 3 lines. I got the AWK part but i cant do the loop part. #!/bin/bash export line=`awk 'END { print NR }' btnew` echo $line for i in {1..$line..3} #increment do echo... (2 Replies)
Discussion started by: theodorosGreece
2 Replies

4. Shell Programming and Scripting

Increment a variable in unix bash

Hello There, I have been trying to increment the value of variable to 1, 2, 3 etc. but, it displays 1 1+1 1+1+1 ..... :wall: Could anyone help out with this? for i in *.* do s=`expr $s+1` echo $s j=$i$j mv $i $j done Any help is appreciated? (24 Replies)
Discussion started by: amrutha0303
24 Replies

5. Shell Programming and Scripting

increment counter as suffix starting with the rightmost digit

Hi, I would like to add a suffix to a file name but maintain the suffix length to 5 digits. For eg, output > 1st_file.00001, 2nd_file.00002...10th_file.00010....100th_file.00100 Can anyone please advise me on how to go about it? Platform: SunOS mps201a 5.9 Generic_118558-39 sun4u... (7 Replies)
Discussion started by: danish0909
7 Replies

6. Shell Programming and Scripting

loop with a counter on a constant in bash

Hello Everyone, I'm in need of assistance on creating a script with a counter on a certain string. Basically this script opens a log file and displays certain log data. There are two key words in the log. START and FINISH. In between the START and FINISH is a variable ACTNUMBER. It will... (1 Reply)
Discussion started by: rxc23816
1 Replies

7. UNIX for Dummies Questions & Answers

bash script to increment a digit in filename

Hi guys, Can someone help me out with this: I have a directory with files like the following, GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g ... (4 Replies)
Discussion started by: hbjlee17
4 Replies

8. Shell Programming and Scripting

Increment counter in ksh

Hello, I am making an increment counter in ksh. So i write a number in a temporary file and when I execute my script I read this file and I make +1. But how can I make this with a number with 6 digits , example 000009 + 1 = 000010 ... .... .... 000099 + 1 = 000100 Do anyone know... (5 Replies)
Discussion started by: steiner
5 Replies

9. UNIX for Dummies Questions & Answers

counter / increment problem within echo stmt

Simple script trying to increment a counter within an echo statement never gets past 1 - PLEASE HELP! Thanks. ~~~~~~~~~~~ #!/bin/sh stepup() { STEP=`expr $STEP + 1` echo $STEP } # # Initialize variables # STEP=0 echo "Counter Value: `stepup`" echo "Counter Value:... (2 Replies)
Discussion started by: blaze
2 Replies
Login or Register to Ask a Question