How to comment out blocks of code in shell scripts


Normally, to comment out lines in a shell script, one inserts a pound sign at the beginning of the line. That's fine, but that can be a lot of work if there are several lines to be commented out. Another way is to use a variation on the here-document to use indicate a beginning and end of commented-out code, similar to C's /** and **/.

#!/bin/bash :<< COMMENTBLOCK All the lines within this block are commented out, that is, the shell won't try to execute them even though they don't have a # in front of them. Pretty cool, huh! COMMENTBLOCK echo "hello world"

When you run this script, you'll just get 'hello world', everything within the COMMENTBLOCK words will be ignored.

09/21/2004