-1

In

for (( expr1 ; expr2 ; expr3 )) ; do commands ; done

expr1, expr2, and expr3 are arithmetic expressions.

  • Is expr1 ; expr2 ; expr3 not an arithmetic expression? (( expr1 ; expr2 ; expr3 )) isn't a stand-alone command, so I guess expr1 ; expr2 ; expr3 isn't an arithmetic expression. Note that expr1, expr2, expr3 is an arithmetic expression.

  • Are (( and )) in the for-loop different from (( and )) in command (( 3 ))?

  • Does (( expr1 ; expr2 ; expr3 )) make sense only in for-loop? Or also else where?

1
  • 1
    I appreciate any upvote to counteract the unreasonable downvotes. Commented Apr 24, 2016 at 23:17

1 Answer 1

3

expr1; expr2; expr3 is not an arithmetic expression since ; is not a possible operator in arithmetic expressions, as you can see by reading the syntax of arithmetic expressions in the bash manual.

The syntax of for loops gives the form for ((expr1; expr2; expr3)) ; do commands ; done, with the semicolons. So the ((…)) bit is not the same thing as an arithmetic instruction, which doesn't start with for.

((expr1; expr2; expr3)) is part of the syntax of for loops. It's only a valid syntactic unit after for.

6
  • @BinaryZebra Arithmetic expression is $(()), not ((…)). ((…)) doesn't appear to have a standard name. Commented Apr 24, 2016 at 22:38
  • It is difficult to me to single out to which of all three uses of (( you are referring to in your sentence. I believe that the most common name for a simple (( )) is "compound command", as the manual states: LESS=+'/Compound Commands' man bash. But "arithmetic instruction" seems to be a new name that doesn't seem to focus on any of them. Commented Apr 24, 2016 at 22:54
  • @BinaryZebra “Compound command” and “conditional construct” is a general term that encompass many things beyond ((…)). “Conditional construct” is not a standard term and does not make much sense for ((…)) since it has a usual meaning in programming languages which in the shell would cover if and case, and furthermore “conditional expression” is a standard term for a different thing (what's inside [ … ] or [[ … ]]). Commented Apr 24, 2016 at 23:08
  • I did not use “conditional construct” in my comment. And, there is only one possible “Compound command” that looks like (( )), less confusion possible. But if you want to keep your answer as it is, It is all fine with me, no need for anything else. Commented Apr 24, 2016 at 23:17
  • @BinaryZebra for ((…; …; …)); do …; done is also a compound command. Commented Apr 24, 2016 at 23:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.