Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Increment a variable in unix bash

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-24-2012
amrutha0303 amrutha0303 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 1 March 2016, 12:13 AM EST
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
.....
Linux
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 Unix and Linux 04-24-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 December 2016, 5:18 PM EST
Location: Saskatchewan
Posts: 21,658
Thanks: 1,008
Thanked 3,980 Times in 3,697 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 Unix and Linux 04-24-2012
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 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 Unix and Linux 04-24-2012
amrutha0303 amrutha0303 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 1 March 2016, 12:13 AM EST
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 Unix and Linux 04-24-2012
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 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 Unix and Linux 04-24-2012
amrutha0303 amrutha0303 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 1 March 2016, 12:13 AM EST
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!!LinuxLinux
Sponsored Links
    #7  
Old Unix and Linux 04-24-2012
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 Posts
If it's bash, it shouldn't throw an error with this code Linux


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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 01:39 PM
bash script to increment a digit in filename hbjlee17 UNIX for Dummies Questions & Answers 4 03-14-2010 05:06 PM
Increment of a variable learning_skills Shell Programming and Scripting 4 12-02-2009 11:32 AM
Increment variable stored in a file solitare123 Shell Programming and Scripting 1 07-09-2008 02:36 AM
increment a Variable cengiz Shell Programming and Scripting 4 07-13-2005 06:31 AM



All times are GMT -4. The time now is 12:15 PM.