阅读 LTP shell code它使用奇怪的 for
循环语法:
for arg; do
TCID="${TCID}_$arg"
done
- 它如何接受参数?我希望它循环遍历
$arg
,用$IFS
分隔,但是当尝试$arg="aa bb"; 时对于精氨酸;做 echo $arg;完成
,它什么都不打印。 - 这是一个 bashism 吗?
最佳答案
这是一种使用 for
循环处理命令行选项的特殊方式。
相当于:
for arg in "$@"; do
TCID="${TCID}_$arg"
done
它不特定于 bash。它在 POSIX 中定义:
The for Loop
The for loop shall execute a sequence of commands for each member in a list of items. The
for
loop requires that the reserved wordsdo
anddone
be used to delimit the sequence of commands.The format for the for loop is as follows:
for name [ in [word ... ]] do compound-list done
First, the list of words following
in
shall be expanded to generate a list of items. Then, the variable name shall be set to each item, in turn, and thecompound-list
executed each time. If no items result from the expansion, thecompound-list
shall not be executed. Omitting:in word ...
shall be equivalent to:
in "$@"
关于bash - Linux 外壳 : for arg; do,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73134672/