6

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?

  • 6
    when a "#" is followed by a "!", it is not a comment, but a shebang. see this question from yesterday: http://askubuntu.com/questions/460981/what-is-this-line-at-the-top-of-many-files/460998#460998 – Jacob Vlijm May 06 '14 at 07:31
  • 1
    @Sneetsher: This is not a duplicate. The OP means the # in /boot/grub/menu.lst file. – jobin May 06 '14 at 18:54
  • the first example is not exactly the best one, this is about something else – arsaKasra May 06 '14 at 22:17

2 Answers2

19

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)

jobin
  • 27,708
  • Sorry, I mean exactly this: http://superuser.com/questions/35984/booting-up-renders-error-ata1-00-revalidation-failed-errno-5-in-ubuntu – user3390767 May 06 '14 at 07:48
  • @user3390767: See the update. – jobin May 06 '14 at 08:04
  • @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 06 '14 at 16:01
  • @Ruslan: Thanks for pointing that out, updated. – jobin May 06 '14 at 16:22
  • It is always a comment to the interpreter, regardless of how the process gets started. That is, the kernel doesn't hide the first line of the file from the interpreter when you do ./file - the interpreter still sees it. – nobody May 06 '14 at 18:07
  • Yes, just the loader does parse this line, so for it it's not a comment. – Ruslan May 06 '14 at 18:49
  • +1: This is very useful. You might consider adding the correct term (shebang) to the answer as well :) – Amal Murali May 18 '14 at 18:39
  • @AmalMurali: Thanks for pointing that out, done! – jobin May 18 '14 at 18:41
2

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.

thomasrutter
  • 36,774