I was wondering if there is a way to comment out a set of lines in a shell script. How could I do that? We can use /* */ in other programming languages. This is most useful when I am converting/using/modifying another script and I want to keep the original lines instead of deleting.
It seems a cumbersome job to find and prefix # for all the lines which are not used.
Lets say there are 100 lines in the script in consequent lines which are not to used. I want to comment them all out in one go. Is that possible?
comment and uncomment a line with Shell Script Requirement is: 1. comment and uncomment the line with Shell Script: /opt/admin/fastpg/bin/fastpg.exe -c -=NET (using fastpg.exe as a search option) 2.
First way: Select the multiple lines which you want to comment using the cursor and then use the key combination “control + shift + C” to comment or uncomment the selected lines.
The most versatile and safe method is putting the comment into a void quoted here-document
, like this:
<<"COMMENT" This long comment text includes ${parameter:=expansion} `command substitution` and $((arithmetic++ + --expansion)). COMMENT
Quoting the COMMENT
delimiter above is necessary to prevent parameter expansion, command substitution and arithmetic expansion, which would happen otherwise, as Bash manual states and POSIX shell standard specifies.
In the case above, not quoting COMMENT
would result in variable parameter
being assigned text expansion
, if it was empty or unset, executing command command substitution
, incrementing variable arithmetic
and decrementing variable expansion
.
Comparing other solutions to this:
Using if false; then comment text fi
requires the comment text to be syntactically correct Bash code whereas natural comments are often not, if only for possible unbalanced apostrophes. The same goes for : || { comment text }
construct.
Putting comments into a single-quoted void command argument, as in :'comment text'
, has the drawback of inability to include apostrophes. Double-quoted arguments, as in :"comment text"
, are still subject to parameter expansion, command substitution and arithmetic expansion, the same as unquoted here-document contents and can lead to the side-effects described above.
Using scripts and editor facilities to automatically prefix each line in a block with '#' has some merit, but doesn't exactly answer the question.
if false then ...code... fi
false
always returns false so this will always skip the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With