我忘记了 bash 循环的语法和语义for
,并尝试在系统中查找手册。我找不到它。它不在man bash
或中man builtins
。我在哪里可以快速本地参考bash
命令?
答案1
也许您需要比手册页更多的细节。以下是一些示例
从man bash
:
for name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of
items. The variable name is set to each element of this list in
turn, and list is executed each time. If the in word is omit‐
ted, the for command executes list once for each positional
parameter that is set (see PARAMETERS below). The return status
is the exit status of the last command that executes. If the
expansion of the items following in results in an empty list, no
commands are executed, and the return status is 0.
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
First, the arithmetic expression expr1 is evaluated according to
the rules described below under ARITHMETIC EVALUATION. The
arithmetic expression expr2 is then evaluated repeatedly until
it evaluates to zero. Each time expr2 evaluates to a non-zero
value, list is 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 expres‐
sions is invalid.
例子:
for i in *.txt;do echo textfiles: $i;done
for (( i=1; i<=20 ; i++ )) ; do echo $i;done
for i in *.TXT;do mv $i ${i/\.TXT/}.txt; done # "move *.TXT *.txt"