简体   繁体   中英

Comment in a bash loop argument list

I want to comment parts of a bash for loop argument list. I would like to write something like this, but I can't break the loop across multiple lines. Using \\ doesn't seem to work either.

for i in
  arg1 arg2     # Handle library
  other1 other2 # Handle binary
  win1 win2     # Special windows things
do .... done;

You can store your values in an array and then loop through them. The array initialization can be interspersed with comments unlike line continuation.

values=(
    arg1 arg2     # handle library
    other1 other2 # handle binary
    win1 win2     # Special windows things
)
for i in "${values[@]}"; do
    ...
done

Another, albeit less efficient, way of doing this is to use command substitution. This approach is prone to word splitting and globbing issues.

for i in $(
        echo arg1 arg2     # handle library
        echo other1 other2 # handle binary
        echo win1 win2     # Special windows things
); do
  ...
done

Related:

In the code beneath I do not use handlethings+= , it will be too easy to forget a 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM