Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block Comments in a Shell Script

Tags:

shell

People also ask

How do I comment in a bash script?

When writing bash scripts, any text after a hash sign ( # ) indicates the start of a comment, and any text after # in the same line does not execute. When using a text editor or IDE, comments are colored differently from the rest of the code.

How do you uncomment multiple lines in shell script?

Press CTRL + V to enable visual block mode. Move down and select the lines till you want to uncomment. press x and it will uncomment all the selected lines at once.

What is comments in shell script?

A word or line beginning with # causes that word and all remaining characters on that line to be ignored. These lines aren't statements for the bash to execute. In fact, the bash totally ignores them. These notes are called comments. It is nothing but explanatory text about script.

How do you comment out a block of code in Linux?

You start your editor, navigate down to the beginning row of the block you want to comment out. You hit i to go into insert mode, enter // to comment, hit ESC to go back to command mode, hit j to navigate down to the next row, and then repeat until all the rows are commented out.


In bash:

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

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

For an explanation, see this and this question.


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 anywhere where there is /bin/sh.


You can use:

if [ 1 -eq 0 ]; then
  echo "The code that you want commented out goes here."
  echo "This echo statement will not be called."
fi

Use : ' to open and ' to close.

For example:

: '
This is a
very neat comment
in bash
'

This is from Vegas's example found here