Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

By a commented line I mean either:

#!/usr/bin/perl -w

and the comments in /boot/grub/menu.lst file as described in this answer on superuser.

It already has a comment, i.e the line begins with a #, but still is being interpreted. Why is that so?

share|improve this question

marked as duplicate by Sneetsher, Mitch May 6 at 18:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6  
when a "#" is followed by a "!", it is not a comment, but a shebang. see this question from yesterday: askubuntu.com/questions/460981/… –  Jacob Vlijm May 6 at 7:31
1  
@Sneetsher: This is not a duplicate. The OP means the # in /boot/grub/menu.lst file. –  i08in May 6 at 18:54
    
the first example is not exactly the best one, this is about something else –  arsaKasra May 6 at 22:17
add comment

3 Answers 3

Any line that begins with a # is a comment in many languages and is ignored by the interpreter (perl etc.).

However, if the first line of a script in Linux begins with a #! (shebang as it is called), it is not a comment but a directive to the program loader to actually run the program specified after #! and pass it the name of your file as the last argument.

For example, if the first line is

#!/usr/bin/perl -w

it means the shell will actually invoke /usr/bin/perl -w /path/to/the/script and you don't need to specify a program to run this script, you can run it using

/path/to/the/script

if you have the permission to run it and it is located on a filesystem supported for execution and the file has the permission to be executed.

For the interpreter, however, this line is always just a comment, so if the script is executed as:

perl /path/to/the/script

then the line has no effect. (Thanks to Ruslan for pointing this out).

Be warned that # is not always indicative of a comment. For example, a statement beginning with a # in C is pre-processor directive and not a comment.

In your case, the line is a comment and will be ignored while execution.

Update:

The file you are talking about is a menu.lst for which a comment is a line beginning with ## and not #. (Source)

share|improve this answer
    
Sorry, I mean exactly this: superuser.com/questions/35984/… –  user3390767 May 6 at 7:48
    
@user3390767: See the update. –  i08in May 6 at 8:04
    
yes I see, thanks! –  user3390767 May 6 at 8:11
    
@Jobin maybe add that if one runs the script as perl /path/to/the/script, then this first line is a comment and doesn't change the way perl is launched. –  Ruslan May 6 at 16:01
    
@Ruslan: Thanks for pointing that out, updated. –  i08in May 6 at 16:22
show 4 more comments

The line beginning with #! is still a comment, in that it is not executed as normal commands.

But not only is it a valid comment but it's also a hashbang, a line that can be used to indicate the interpreter to be used to execute these commands if the script is called on its own.

Hashbangs begin with a # in order to be backward-compatible with interpreters that don't read hashbangs, in which case they will simply be interpreted as comments and ignored.

share|improve this answer
add comment

What you mean is probably something like this:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#

This example is from the /etc/fstab file. The lines starting with # here are meant to help you how to correctly use the file. They do not have an actual function other than that.

in some files, more than one # is used before commented lines:

## comment line one.
## comment line two with more information

The first example of your question: #!/usr/bin/perl -w however, is a shebang, see this question for more about that subject.

These are the general rules for commenting. However in some (mostly language-specific) files, exceptions are possible to the general rules. In PHP for example, comments are made between /* */

share|improve this answer
    
I mean this, please visit the ansver here: superuser.com/questions/35984/… –  user3390767 May 6 at 7:45
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.