Cynosure.X International LLC


Add Comment | Related Links | TrackBack
Related Content

Multi-Line Comments in Shell Script

The UNIX shell script is a powerful beast. You can pretty much write anything in shell scripts that you can also write in other languages. The thing is that UNIX shell script only support single line comments, with a hash (#) mark. So to comment out the following example, you'd have to put a hash mark in front of every line:

# if [ -f "${distroFile}" ]
# then
# cp "${distroFile}" "${tmpDir}"
# else
# echo "*** ${distroFile} does not exist!"
# exit
# fi

Well, that's a lot of work. As you can see in the example above, that's seven hash marks. And you'd have to change the cursor position for every hash mark. Don't you wish there is a C or Java-like multi-line comment (/* */) sequence?

Well, today, I got the bright idea of comment out multi-line shell script source codes by surrounding them in function calls. The same example above would be changed to the following:

comment1 ()
if [ -f "${distroFile}" ]
cp "${distroFile}" "${tmpDir}"
echo "*** ${distroFile} does not exist!"

Now, isn't that must easier and neater?

Chieh Cheng
Tue, 29 Apr 2008 17:36:57 +0000

This is another way:

echo "This will not get printed"
echo "Yet another escape"

Reference: . . .

Jatin Sharma
Mon, 16 Jun 2008 16:46:56 +0000

Really useful considering the behavior of the vi editor in Unix/Linux.

Good Stuff. Thank you

Venkatagiri Vatlam
Thu, 25 Sep 2008 14:14:28 +0000

csh doesn't have functions. But Jatin's supercalifragilisticexpialidocious example works great in csh.

The function method works even if you put the multi-line comments inside another function.

Chieh Cheng
Fri, 03 Jul 2009 17:55:39 +0000

Thanks, i served too!!


Hans Ancieta
Thu, 14 Apr 2011 14:55:58 +0000

Nice style of commenting....looks neat and classy...;)

Thu, 20 Oct 2011 09:47:25 +0000

echo -n "Enter some text > "
read text
echo "You entered: $text"
echo "Line after multiline comments" >>>>>>this is echo in not getting print after multiline comments.

but comment(){} its working ...really nice and neat trick.

Wed, 16 Nov 2011 11:21:36 +0000

I was laughing when I saw this. Real Neat. Good Job !

Wed, 30 Jan 2013 11:45:19 +0300

well, apparently if I have developer's musing that I want to record this method wouldn't work, right? - cause stuff like a TODO list that I want to maintain as part of a future release wouldn't be allowed under a comment unless i qualify every statement that I write there by an echo command.

Wed, 06 Feb 2013 21:27:02 +0300

Or create a specific function for comments (this functions does nothing, by the way):

comment() { :; }

now you can enter comments anywhere with any amount of lines:
echo "hello worlds"
comment "this is a comment"
echo "bye cruel worlds"
comments "
this is a
multi line

Donat Callens
Thu, 21 Feb 2013 12:25:02 +0300

Add Comment | Related Links | TrackBack
Related Content

Did your message disappear? Read the Forums FAQ.

Add Comment

Spam Control | * indicates required field
Your Name: *
Remember Me!
Comment: *
File attachment is optional. Please do not attach a file to your submission unless it is relevent.
Attach File:
(20 MB Max)
Spam Protection: * Answer of 6 + 4?
Click button only once, please!


TrackBack only accepted from WebSite-X Suite web sites. Do not submit TrackBacks from other sites.

Send Ping | TrackBack URL | Spam Control

No TrackBacks yet. TrackBack can be used to link this thread to your weblog, or link your weblog to this thread. In addition, TrackBack can be used as a form of remote commenting. Rather than posting the comment directly on this thread, you can posts it on your own weblog. Then have your weblog sends a TrackBack ping to the TrackBack URL, so that your post would show up here.

Messages, files, and images copyright by respective owners.

Products | Services
Forums | Latest | RSS
Library | Search | Wiki
Help | Licenses

Login | Register

52 Users Online

Hacking Digital Cameras
Fun for Photographers

Amazon Associate

Copyright © 1996 - 2024. All Rights Reserved.