[SOLVED] Bash: syntax for ((<arithmetic expression>)) usage?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
[/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.
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.
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.
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:
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>.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.