views:

957

answers:

4

Is there a simple way to comment out a block of code in a shell script?

Google has some suggestions but they all seem awkward and ugly.

+5  A: 

In bash:

#!/bin/bash
echo before comment
: <<'END'
bla bla
blurfl
END
echo after comment

The '' around the END delimiter is important, otherwise things inside the block like for example $(command) will be parsed and executed.

sunny256
Cute trick - as long as the 'END' keyword (which is, of course, user chosen) does not appear on a line on its own inside the material to be commented out.
Jonathan Leffler
A: 

Find a good text editor. Newlines are usually the most effective comment-end tags, given an effective text editor.

wilhelmtell
+2  A: 

There is no block comment on shell script.

Using vi ( yes vi ) you can easily comment from line n to m

<ESC>
:10,100s/^/#/

( that reads, from line 10 to 100 substitute line start (^) with a # sign. )

and un comment with

<ESC>
:10,100s/^#//

( that reads, from line 10 to 100 substitute line start (^) followed by # with noting //. )

vi is almost universal on anywhere where there is /bin/sh

:)

OscarRyz
+1  A: 

In Vim:

  1. go to first line of block you want to comment
  2. shift-V (enter visual mode), up down highlight lines in block
  3. execute the following on selection :s/^/#/
  4. the command will look like this:

      :'<,'>s/^/#
    
  5. hit enter

e.g.

shift-V
jjj
:s/^/#
<enter>
stefanB