3

In Bash Manual, sec 6.5 Shell Arithmetic

expr1 , expr2
comma

What does the comma operator do?

Are expr1 and expr2 arithmetic expressions?

8

, is a list operator.

The list of arithmetic expressions will be evaluated from left to right, the last expression result is the return value:

$ echo "$(( a=1, ++a, ++a ))"
3

The , list operator was added in bash-2.04-devel (along with pre/post increment/decrement operators).

You may want to read expr.c to see how other operators were implemented, and function expcomma() for , operator.

  • 1
    thanks. does the manual say that? – StackExchange for All Mar 19 '16 at 6:25
  • 1
    which reference can I find it? – StackExchange for All Mar 19 '16 at 7:36
  • @Tim, very interesting; I can't find it at all anywhere in the bash man page or anywhere in the POSIX specs! – Wildcard Mar 19 '16 at 15:59
  • How did you know? @C – StackExchange for All Mar 19 '16 at 17:05
  • @Tim: See the bash changelog, search for comma operator. The bash documentation is not clear about it. – cuonglm Mar 20 '16 at 12:27
4

Bash

The comma operator is valid in bash (LESS=+/'expr1 , expr2' man bash):

expr1 , expr2
comma

And no other explanation of its use.
For that we have to read the "C Language" description (where all this arithmetic operators were born).

Example (as in the link above) (the second of which has the value 5):

$ echo "$(( (t=3, t+2) )) $t"
5 3

Or, simpler:

$ echo "$(( t=3, t+2 )) $t"
5 3

Yes each element separated by a comma may be an expression:

1313    expression:
            assignment-expression
            expression , assignment-expression

POSIX

POSIX does not include a comma operator:
Table: Selected ISO C Standard Operators and Control Flow Keywords

Or just try:

$  dash -c 'echo $(( t=3,t+2 ))'
dash: 1: arithmetic expression: expecting EOF: " t=3,t+2 "
$  bash -c 'echo $(( t=3,t+2 ))'
5

As IBM confirm in its page for AIX and POSIX shell (emphasis mine):

All of the integral operators, other than ..., and comma (,), are supported.

But, also in AIX, IBM claim support for comma operator from the ksh93 shell at /usr/bin/ksh93

More arithmetic operators are available, including the unary +, ++, --, and the ?: construct (for example, "x ? y : z"), as well as the , (comma) operator.

Conclusion:

dash, ksh   do not have a comma operator.
ksh93          may have a comma operator.
bash, zsh   do     have a comma operator.
  • Thanks. Why doesn't echo "$(( t=3, t+2 )) $t" output 5 3? – StackExchange for All Mar 19 '16 at 18:32
  • A typo, check again :-) – user79743 Mar 19 '16 at 18:52
  • What's the extra parentheses in the previous example for? Why are the results the same with and without them? – StackExchange for All Mar 19 '16 at 19:28
  • The same way as in school math: (2+3) is 5 as ((2+3)) is 5, as 2+3 is also 5. Additional parenthesis just help collect together some elements. They are discarded after use (if not needed to parse the equation). – user79743 Mar 19 '16 at 20:45
  • 1
    @Tim A thanks, So many questions and not even one upvote? Shame on you. – user79743 Mar 19 '16 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.