LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 03-18-2010, 02:24 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Bash: syntax for ((<arithmetic expression>)) usage?


Hello

I do not understand bash' syntax regarding the use of ((<arithmetic expression>)).
  1. $((<arithmetic expression>)) is tokenised to a single word. OK.
    .
  2. ((<arithmetic expression>)) may be used in some places:
    1. In array indexing
      Code:
      array[((<arithmetic expression>))]=value
    2. In isolation
      Code:
      ((<arithmetic expression>))
  3. It may not be used where a word is expected. This generates a syntax error. Why?
    Code:
    echo ((<arithmetic expression>))
Best

Charles
 
Old 03-18-2010, 03:05 AM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Fife, WA
Distribution: Fedora
Posts: 435
Blog Entries: 3

Rep: Reputation: 59
Code:
echo $(( <arithmetic expression> ))
You must use a dollar sign in front of the expression where a string would be expected. Otherwise it does not send anything to standard output. For instance, you can place an arithmetic expression in place of any other command on a line by itself:
Code:
(( var++ ))
This is a valid expression. It increments var, but sends nothing to standard output. The dollar sign forces the result of the expression to standard output. So, this would output the result of incrementing var:
Code:
echo $(( ++var ))
 
Old 03-18-2010, 03:21 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by catkin View Post
[/LIST][*]It may not be used where a word is expected. This generates a syntax error. Why?
Code:
echo ((<arithmetic expression>))
[/LIST]Best

Charles
((i++)) increment the value of i. if you want to echo out the value, you need to put "$" in front, ie "echo $((i++))", just like any other shell variable interpolation.
 
Old 03-18-2010, 03:45 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,002

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
Hi, Charles. I second previous posts. Just take in mind that the $ sign is the way by which the shell retrieves the value of a variable, an expression, a substitution and so on.
 
1 members found this post helpful.
Old 03-18-2010, 06:48 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
I agree with colucix; it's more accurately value retrieval, rather than sending to stdout, although the effect is pretty much the same, at least for echo.
 
1 members found this post helpful.
Old 03-18-2010, 10:11 AM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 1,997

Rep: Reputation: 107Reputation: 107
Indeed, $(()) is a substitution just like ${}. (()) isn't, so it can't be used where you really need a substitution.

BTW, contexts that are already arithmetic expressions don't need the (()). They don't hurt, but what you're really doing is parenthesizing the whole expression twice. Note:
Code:
tim@epoch:~ >foo[4+5]=foo
tim@epoch:~ >echo "${foo[9]}"
foo
 
1 members found this post helpful.
Old 03-18-2010, 10:58 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Original Poster
Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Thanks to all

That has really helped. It's very useful to have a clear picture of how these things work and I was stumbling around in the woods, looking for trees.

If I may test my new understanding by summarising:
  • ((<arithmetic expression>)) is a command. It evaluates <arithmetic expression> which can be used to assign values and to increment and decrement values.
  • $((<arithmetic expression>)) runs the same command and the shell, while tokenising, substitutes the value of the <arithmetic expression>.
  • In places where an arithmetic expression is required, ((<arithmetic expression>)) may be used but there is no benefit over using <arithmetic expression>.
OK?
 
  


Reply

Tags
bash, syntax


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeset arithmetic syntax error rosariop Linux - Newbie 7 10-13-2009 08:27 AM
Contents of Directory Displayed using Arithmetic Expression Mr. ameya sathe Programming 3 01-26-2008 11:04 AM
bash, for loop, and arithmetic rmount Programming 13 05-23-2007 01:27 AM
Bash arithmetic Blackout_08 Programming 2 06-08-2006 10:37 PM
simple arithmetic in bash gfrair Linux - Newbie 9 03-16-2005 02:09 PM


All times are GMT -5. The time now is 11:17 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration