Thursday, November 21, 2013

Comment out block of code in bash shell script

Credit goes to this post on Stack Overflow. I don't fully understand the syntax, but it works.
#!/bin/bash
echo "Before commented block"
: <<'END'
command 1 # Doesn't get executed
command 2 # Doesn't get executed
END
echo "After commented block"
Another commenter pointed out that the string 'END' does not have a specific meaning and any string can be used. In fact, it may be advantageous to use a different string that you know won't be present in your block of code. Much to my amusement, he suggested SNURFLE_BURGERS:
: <<'SNURFLE_BURGERS'
echo this is not executed
SNURFLE_BURGERS
I'm torn as to which naming convention to adopt.

No comments:

Post a Comment