Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to comment out particular lines in a shell script

Tags:

shell

unix

sh

Can anyone suggest how to comment particular lines in the shell script other than #?

Suppose I want to comment five lines. Instead of adding # to each line, is there any other way to comment the five lines?

like image 434
user2400564 Avatar asked Aug 21 '13 10:08

user2400564


People also ask

How do I comment out a specific line in a shell script?

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.

How do you comment multiple lines in a shell script?

In Shell or Bash shell, we can comment on multiple lines using << and name of comment. we start a comment block with << and name anything to the block and wherever we want to stop the comment, we will simply type the name of the comment.

How do you comment out in a script?

Single line comments start with // . Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

How do you comment out 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.


2 Answers

You can comment section of a script using a conditional.

For example, the following script:

DEBUG=false if ${DEBUG}; then echo 1 echo 2 echo 3 echo 4 echo 5 fi echo 6 echo 7 

would output:

6 7 

In order to uncomment the section of the code, you simply need to comment the variable:

#DEBUG=false 

(Doing so would print the numbers 1 through 7.)

like image 76
devnull Avatar answered Oct 29 '22 12:10

devnull


Yes (although it's a nasty hack). You can use a heredoc thus:

#!/bin/sh  # do valuable stuff here touch /tmp/a  # now comment out all the stuff below up to the EOF echo <<EOF ... ... ... EOF 

What's this doing ? A heredoc feeds all the following input up to the terminator (in this case, EOF) into the nominated command. So you can surround the code you wish to comment out with

echo <<EOF ... EOF 

and it'll take all the code contained between the two EOFs and feed them to echo (echo doesn't read from stdin so it all gets thrown away).

Note that with the above you can put anything in the heredoc. It doesn't have to be valid shell code (i.e. it doesn't have to parse properly).

This is very nasty, and I offer it only as a point of interest. You can't do the equivalent of C's /* ... */

like image 42
Brian Agnew Avatar answered Oct 29 '22 12:10

Brian Agnew