LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2012, 08:29 AM   #1
garion
Member
 
Registered: Nov 2003
Posts: 35

Rep: Reputation: 15
Numeric addition in bash


I am writing a script that will retry an action a number of times before it fails and I discovered both of these work:

RETRY=$(( $RETRY + 1 ))
RETRY=$(( RETRY + 1 ))

It also seems that they behave the exact same way. The first example makes sense, but not the second. I googled, but I could not find an explanation for how or why the second would work.

Any info would be appreaciated.
 
Old 09-06-2012, 08:35 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,109

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
see here (for example):
http://www.bashguru.com/2010/12/math...l-scripts.html
it works: bash knows RETRY cannot be a number, so it is a variable.
 
1 members found this post helpful.
Old 09-06-2012, 08:46 AM   #3
garion
Member
 
Registered: Nov 2003
Posts: 35

Original Poster
Rep: Reputation: 15
So bash is simply smart enough to know what it is supposed to be. Cool! Thank you very much for the quick response.

And screw those windows-weanies who claim there is no support for Linux!
 
Old 09-06-2012, 09:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You can also do:
Code:
((RETRY++))
 
1 members found this post helpful.
Old 09-07-2012, 02:53 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
It's not just bash. All posix-based shells should work in basically the same way. The ((..)) construct is only available in bash/ksh/zsh, however.

See here for much more on shell arithmetic: arithmetic expressions

Edit:

Note that the shell is only smart enough to determine if a variable contains a numeral or not. For other operations you need to use the "$".

Code:
#build an entire expression from variables
$ a=4 b=+ c=3
$ echo $(( a b c ))
bash: +: syntax error: operand expected (error token is "+")
$ echo $(( a $b c ))
7

#add the total number of array entries to the number in the last entry
$ array=( 1 2 3 )
$ echo "$(( array[2] + ${#array[@]} ))"
6

Last edited by David the H.; 09-07-2012 at 03:05 AM. Reason: as stated
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: how to compare REAL numeric values? muebi Linux - General 3 02-28-2012 09:00 AM
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 11:11 AM
[SOLVED] bash thinks string variable is a numeric one. stf92 Programming 11 07-08-2011 02:35 AM
In BASH: How to read + define + modify a numeric value from/to a file. jdaoutid Programming 3 03-26-2010 12:23 PM
Bash script with addition... mijohnst Linux - General 3 04-20-2007 11:14 AM


All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration