Increment a variable in unix bash | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Increment a variable in unix bash

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-24-2012
amrutha0303 amrutha0303 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 24 April 2012, 3:06 PM EDT
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
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
.....

Could anyone help out with this?

Code:
for i in *.*
do
s=`expr $s+1`
echo $s
j=$i$j
mv $i $j  
done

Any help is appreciated?

-A
Sponsored Links
    #2  
Old 04-24-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 August 2014, 5:32 PM EDT
Location: Saskatchewan
Posts: 19,236
Thanks: 773
Thanked 3,224 Times in 3,022 Posts
If you're really in BASH:


Code:
((S++))

will suffice.

If you want to use expr, remember that each thing you feed expr must be a separate argument. That means putting in spaces.


Code:
S=`expr $S + 1`

Sponsored Links
    #3  
Old 04-24-2012
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 22 August 2014, 4:46 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,666
Thanks: 182
Thanked 617 Times in 575 Posts
If you're running and old bash version, you'll need a bit different syntax:


Code:
$ bash -c 'echo $BASH_VERSION; (( c++ ))'
2.03.0(1)-release
bash: ((: c++ : syntax error: operand expected (error token is " ")
$ bash -c 'echo $BASH_VERSION; (( c += 1 )); echo $c'
2.03.0(1)-release
1

The Following User Says Thank You to radoulov For This Useful Post:
amrutha0303 (04-24-2012)
    #4  
Old 04-24-2012
amrutha0303 amrutha0303 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 24 April 2012, 3:06 PM EDT
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
Hello there,

Thanks both for the quick reply. I tried using (( C +=1 )) and it kept throwing a syntax error at me. I was just not sure why.

I am curious though. Functions like
Code:
$(( ls - l ))

as well doesnt work for me. It keep throwing the syntax error saying `(' unexpected whenever i use something like that.

Help is appreciated.

Thanks.
-A
Sponsored Links
    #5  
Old 04-24-2012
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 22 August 2014, 4:46 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,666
Thanks: 182
Thanked 617 Times in 575 Posts
Are you sure you're using bash?
What's the output of:


Code:
ps -p$$

and:


Code:
echo $BASH_VERSION

$(( ... )) has a different meaning.
Sponsored Links
    #6  
Old 04-24-2012
amrutha0303 amrutha0303 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 24 April 2012, 3:06 PM EDT
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
Radulov,

Here is the output to your commands:

Code:
ps -p$$

PID TTY TIME CMD
1198 pts/4 0:01 bash


Code:
echo $BASH_VERSION


3.2.51(1)-release


It is bash shell for sure!!
Sponsored Links
    #7  
Old 04-24-2012
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 22 August 2014, 4:46 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,666
Thanks: 182
Thanked 617 Times in 575 Posts
If it's bash, it shouldn't throw an error with this code


Code:
$ bash -c 'echo $BASH_VERSION; (( c += 3 )); echo $c'
3.2.25(1)-release
3

Notice that the standard/POSIX syntax is different:


Code:
c=$(( c + 1 ))

But bash has various extensions.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008) drwatson_droid Shell Programming and Scripting 4 12-01-2011 12:39 PM
bash script to increment a digit in filename hbjlee17 UNIX for Dummies Questions & Answers 4 03-14-2010 04:06 PM
Increment of a variable learning_skills Shell Programming and Scripting 4 12-02-2009 10:32 AM
Increment variable stored in a file solitare123 Shell Programming and Scripting 1 07-09-2008 01:36 AM
increment a Variable cengiz Shell Programming and Scripting 4 07-13-2005 05:31 AM



All times are GMT -4. The time now is 05:37 PM.