LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2012, 04:25 AM   #1
L1nuxn00b703
Member
 
Registered: Sep 2009
Posts: 117

Rep: Reputation: 15
Double (( )) in bash


Hi all,
I'm learning bash scripting and need clarification on the double parentheses in bash. I've read that double-parentheses construct is also a mechanism for allowing C-style manipulation of variables in Bash. Is that all its used for?

Can someone explain in laymans terms.
 
Old 07-16-2012, 04:45 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by L1nuxn00b703 View Post
Can someone explain in laymans terms.
No--a layman will not relate to the differences between BASH and the traditional Bourne shell......

Really, really good idea to have this: http://tldp.org/LDP/abs/abs-guide.pdf on you computer, and use it for searches. In this case, go to section 8.3.

From that section:
Quote:
Quote:
the (( ... )) construct permits arithmetic expansion and evaluation. In its simplest
form, a=$(( 5 + 3 )) would set a to 5 + 3, or 8. However, this double-parentheses construct is also a
mechanism for allowing C-style manipulation of variables.....
 
Old 07-16-2012, 05:48 AM   #3
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
arithmetic expressions

Short form, it evaluates (integer-based) arithmetic expressions inside them and returns an exit code based on the result. This makes it possible to use it in conditional constructs like for, while, and if. The c-style-for loop is one common use of it. You should generally use them any time you want to test for a numerical condition, or apply an expression without having to deal with printed output (e.g. incrementing variables).

The related, posix-compliant structure is $((..)). It also evaluates arithmetic, except that it only prints the resulting values to stdout, and doesn't produce an exit code.

Also see here for the other commonly-used test constructions. In general you should use [[..]] for string and file conditions.

http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/commands/classictest

Last edited by David the H.; 07-16-2012 at 06:01 AM. Reason: fixed link + small addition
 
  


Reply



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
Remove double quotes bash alancampbell6 Programming 4 07-20-2011 03:05 PM
bash and double precision babag Programming 10 04-20-2008 04:19 PM
bash double varables whited Programming 2 10-16-2007 10:42 AM
bash and double slashes MD3 Linux - Software 0 02-18-2005 03:13 PM
Bash double spaces in filenames ilikejam Programming 2 02-01-2005 10:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 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
Open Source Consulting | Domain Registration