3

I am failing to add comments for multi-line statements in bash script. Seems that bash is not interpreting.

Since comments can really be useful because there are potential 4-5 lines, can anyone advice me how to achieve this?

This is just basic example, which is - not working.

#!/bin/bash

iptables -A INPUT \
#Comment for rule bellow
-p tcp --dport 21 \
# Comment for rule bellow no2
-s 10.0.0.1 \
-j ACCEPT

I just give a plain example. Allowing comments can be more easier for complex examples (this is not complicated example, but you got a point) like this:

grep some_file \
#awk does that...
awk '{print $1}' \
#sed does that...
sed 's/match1/match2/g' 

Of course there are no whitechars behind \.

2

A line broken into several lines by escaping the newline is still only one line.

A comment stretches from the # to the end of the line, no matter if that line is broken into many lines or not.

What the shell parses when you write

echo hello \
# world

is

echo hello # world

This is different though (and works):

grep hello |
# now sed:
sed 1p

Since each part of the pipeline is complete on its own line, it's possible to intermingle the lines with comments, as long as the newlines are not escaped.

  • Yes, true, forgot to include | in second example. Thank you for explanation! – fugitive Mar 11 '17 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.