开发者

numeric cycles in shell

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

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

i开发者_高级运维t has strange variable i

where are other constructions for numeric cycling in shells?


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜