LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-18-2010, 02:24 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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: 2,012

Rep: Reputation: 115Reputation: 115
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: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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



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
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

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

All times are GMT -5. The time now is 11:43 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