在 bash 循环参数列表中注释
Comment in a bash loop argument list
我想评论 bash for 循环参数列表的部分内容。我想写这样的东西,但我不能跨多行打破循环。使用 \
似乎也不起作用。
for i in
arg1 arg2 # Handle library
other1 other2 # Handle binary
win1 win2 # Special windows things
do .... done;
您可以将值存储在数组中,然后循环遍历它们。与行连续不同,数组初始化可以穿插注释。
values=(
arg1 arg2 # handle library
other1 other2 # handle binary
win1 win2 # Special windows things
)
for i in "${values[@]}"; do
...
done
另一种虽然效率较低的方法是使用命令替换。这种方法容易出现 word splitting and globbing 问题。
for i in $(
echo arg1 arg2 # handle library
echo other1 other2 # handle binary
echo win1 win2 # Special windows things
); do
...
done
相关:
- Commenting in a Bash script
- How to put a line comment in a multi-line command
- The Open Group Base Specifications - Token Recognition in Shell
在下面的代码中我没有使用handlethings+=
,很容易忘记一个space。
handlethings="arg1 arg2" # Handle library
handlethings="${handlethings} other1 other2" # Handle binary
handlethings="${handlethings} win1 win2" # Special windows things
for i in ${handlethings}; do
echo "i=$i"
done
我想评论 bash for 循环参数列表的部分内容。我想写这样的东西,但我不能跨多行打破循环。使用 \
似乎也不起作用。
for i in
arg1 arg2 # Handle library
other1 other2 # Handle binary
win1 win2 # Special windows things
do .... done;
您可以将值存储在数组中,然后循环遍历它们。与行连续不同,数组初始化可以穿插注释。
values=(
arg1 arg2 # handle library
other1 other2 # handle binary
win1 win2 # Special windows things
)
for i in "${values[@]}"; do
...
done
另一种虽然效率较低的方法是使用命令替换。这种方法容易出现 word splitting and globbing 问题。
for i in $(
echo arg1 arg2 # handle library
echo other1 other2 # handle binary
echo win1 win2 # Special windows things
); do
...
done
相关:
- Commenting in a Bash script
- How to put a line comment in a multi-line command
- The Open Group Base Specifications - Token Recognition in Shell
在下面的代码中我没有使用handlethings+=
,很容易忘记一个space。
handlethings="arg1 arg2" # Handle library
handlethings="${handlethings} other1 other2" # Handle binary
handlethings="${handlethings} win1 win2" # Special windows things
for i in ${handlethings}; do
echo "i=$i"
done