Let's assume the shebang sequence is included in a script that is written in a separate file; for example, I create an example.sh script file.

If I add the shebang sequence in the file:


How am I to be certain that the command above is not being recognised as a comment (in Linux Bash scripting, # is often regarded as the start of a comment.

  • Both answers are the correct answer but I don't know how to mark both as the correct answer.
    – Linuxn00b
    Oct 3, 2015 at 21:28
  • Accept the one that was most helpful or most complete.
    – Wildcard
    Oct 3, 2015 at 22:06

3 Answers 3


A shebang is only really a shebang if the script is executable and "#!" are the first two characters of the file.

The OS is literally looking in the file checking the first two bytes and seeing that they are (in hexadecimal) "23" and "21", which are the ASCII codes for "#" and "!".

If these appear later in the file, or if you're not executing the file directly, the line is indeed treated as a comment because it begins with a #.

In fact, if the line is:


then the OS finds the shebang and therefore runs:

/bin/sh [filename]

and /bin/sh does ignore the shebang line as a comment when running the script.

When it comes to some other languages that doesn't use # as a comment, a special flag is often passed to the interpreter so it knows to ignore the first line (e.g. #!/usr/local/bin/tcc -run).


The original shell did not have this feature, but did have comments. A mechanism was needed, so that a user could specify which shell/interpreter to use. This mechanism must not unnecessarily break old systems (any new scripts written for the original shell, must be allowed to specify the original shell, and must not break on old systems (running early versions of the original shell) that do not know of this new system. Therefore it must look like a comment to the original shell. This new system must also reduce the risk of false triggers, while not putting a burden on future users.

It was therefore decided to add a special comment at the start of the file.


This is a mechanism that was introduced in UNIX shell by Denis Ritchie, so Linux shell did inherit this feature. It is in the shell itself that the first line is checked for this "magic string" and that use the interpreter specified after the shebang if it is found. The mail in which this feature was first mentioned is quoted in some bsd4.0 source code, you can see it here: http://www.in-ulm.de/~mascheck/various/shebang/4.0BSD_newsys_sys1.c.html

  • 2
    This is incorrect; it's the OS that reads the shebang, not the shell.
    – seumasmac
    Oct 3, 2015 at 21:29
  • @seumasmac Incomplete, not incorrect. When the shebang was invented, it was indeed checked by the shell, so it only worked when a script was directly invoked by the shell. Later the feature was added into the kernel, and all modern Unix kernels have it. Oct 3, 2015 at 22:24
  • @Gilles Are you sure? I can't find any reference to that, and it's not what the linked mail says. The only thing I can find which suggests it is a hack in csh to recognise "#" at the start of a file (not looking for the whole shebang) which was later removed.
    – seumasmac
    Oct 3, 2015 at 22:35
  • @seumasmac You are totally right... the job is done by the kernel, source can be checked under fs/binfmt_script.c, the function load_script does the #! interpretation. So this is the kernel that do the job not the shell.
    – herbert
    Oct 4, 2015 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .