1

When i am using the below syntax to iterate why two brackets are needed ?

for (( expr1; expr2; expr3 ))
do
 command1
 command2
 ..
done

and the below code doesn't work ? and throws error "syntax error near unexpected token `('"

for ( expr1; expr2; expr3 )
do
 command1
 command2
 ..
done
1
  • 1
    Those are parentheses, not brackets. Additionally, C doesn't use brackets for blocks, it uses braces. Commented Jan 1, 2015 at 5:15

1 Answer 1

5

The reason for this is ( has a different meaning. From the bash manpage:

        (list) list  is  executed  in  a subshell environment 
               (see COMMAND EXECUTION ENVIRONMENT below).  Variable 
               assignments and builtin commands that affect the shell's 
               environment do not remain in effect  after  the  command  
               completes. The return status is the exit status of list.

        ((expression))
               The expression is evaluated according to the rules 
               described below under ARITHMETIC EVALUATION.  If  
               the  value  of the  expression  is non-zero, the return 
               status is 0; otherwise the return status is 1.  This is 
               exactly equivalent to let "expression".

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.