LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 07: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, 07:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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, 07: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, 08:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You can also do:
Code:
((RETRY++))
 
1 members found this post helpful.
Old 09-07-2012, 01:53 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 02: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 08:00 AM
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 10:11 AM
[SOLVED] bash thinks string variable is a numeric one. stf92 Programming 11 07-08-2011 01:35 AM
In BASH: How to read + define + modify a numeric value from/to a file. jdaoutid Programming 3 03-26-2010 11:23 AM
Bash script with addition... mijohnst Linux - General 3 04-20-2007 10:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:07 PM.

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
Open Source Consulting | Domain Registration