0

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
    Those are parentheses, not brackets. Additionally, C doesn't use brackets for blocks, it uses braces. – HalosGhost Jan 1 '15 at 5:15
4

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

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.