opert opert - 1 year ago 156
Bash Question

Shell scripting: intersperse comments between multiline logical operators

I have a multiline conditional like this:

command1 \
&& command2 \
&& command3

This is fine, but I would like to put comments after each command, like this:

command1 \ # Comment for command 1.
&& command2 \ # Comment for command 2.
&& command3 # Comment for command 3.

but the script breaks (in Bash and Zsh) because of a parsing error.

Is there a way to add comments between multiline conditionals?

NOTE: I realize that I could just create a function for each command, and then just add comments to each command in its function definition; but I would like to avoid that if possible.

Answer Source

No need to escape the comment start. You can use inline comments like this:

pwd && # comment1
date && # comment2
tty # comment3
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download