views:

43

answers:

3

hello, what is the name and syntax of the construction ((..)) in example below?

for ((i=1;i<10;i++)) do echo $i; done

it has strange variable i
where are other constructions for numeric cycling in shells?

+1  A: 

bash(1) man page, SHELL GRAMMAR section, Compound Commands subsection. The other choices for numeric loops are while and until with a manual increment.

Ignacio Vazquez-Abrams
+2  A: 

You can check the Advanced Bash Scripting Guide's section on Loops for more examples of looping constructs besides the C-style for loop you've listed.

Try also:

$ for i in {1..5}; do echo $i; done # range argument
1
2
3
4
5
$ for i in `seq 1 5`; do echo $i; done # iterate over seq command
1
2
3
4
5
Mark Rushakoff
+2  A: 

In section §3.2.4.2 of the 'Bash Reference Manual' (4.0), the '((...))' notation is classified as as an arithmetic expression. It is closely related to the '$((...))' notation described in §3.5.5 as Arithmetic Expansion. And §3.2.4.1 'Looping Constructs' says:

An alternate form of the for command is also supported:

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

First, the arithmetic expression expr1 is evaluated according to the rules described below (see Section 6.5 [Shell Arithmetic], page 78). The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero. Each time expr2 evaluates to a non-zero value, commands are executed and the arithmetic expression expr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command in list that is executed, or false if any of the expressions is invalid.

Jonathan Leffler