Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 705
Chris Jefferson Avatar asked Mar 07 '23 03:03

Chris Jefferson


1 Answers

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:

  • 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
like image 152
codeforester Avatar answered Mar 17 '23 12:03

codeforester