Why does the "she-bang" begin with a #!, like #!/bin/bash? I have always accepted that this how it is done, but is there a reason behind it?

Why start with #; isn't that usually a comment? Or is it the point that it should be comment?

  • 4
    Wikipedia has a fairly detailed history of #! (as much as dmr remembers…), including an explanation of why the # (yes, the line had to be ignored by existing shells). Jul 13, 2011 at 13:08
  • Missed to check wikipedia :)
    – Johan
    Jul 13, 2011 at 13:30
  • 1
    I just wish the shells were smart enough to strip an extraneous CR/LF if it is there... ;) Jul 14, 2011 at 1:02

3 Answers 3


Typically shebang refers to just the #! (! is typically called "bang", and it looks like "she" is a corruption of either "SHArp" or "haSH" for #) -- the whole line is called a shebang line

It does intentionally start with a comment character for backwards-compatibility with things that don't know how to handle it; the ! is presumably just to distinguish it from a random comment starting the file, so a file that begins with # this is my script! doesn't try to run the this is my script! interpreter

  • 2
    Bang ! is often used in other contexts to indicate a command to be executed. For example in vim or other programs with their own command lines, bang is often the escape character that makes them run the command in a system shell instead of their internal interface.
    – Caleb
    Jul 27, 2011 at 11:32

To understand this you must realize that the first line of the script is actually read twice, by 2 different programs. The first time, the kernel opens the file and looks for that character sequence (#!) on the first line. If it finds it, it runs the shell program that is indicated there, passing the file name as a parameter. (e.g. if the file /home/me/foo starts with #!/bin/sh, the kernel will run /bin/sh /home/me/foo).

Next the shell (bin/sh or whatever interpreter program was specified) reads the file. The shell does not know anything about shebang lines but it will still read the first line because it is just like any other line in the file... it reads them all. You don't want the shell to crash or alter its behavior in any way... the way to do that is to make it treat is as a comment and ignore it. Thus, the best character for a kernel instruction to start with would be the comment character.

  • Is it really the kernel that does that, it sounds like it is the current shell you work in that will parse the shebang and then push the script
    – Johan
    Nov 6, 2013 at 11:23
  • Nope... it's the kernel itself, live and in person. It will work even if you are not using a shell to execute the script... there are other ways to execute a file besides a shell... for example from a C program, using the "exec" system call
    – JoelFan
    Nov 6, 2013 at 15:28
  • en.wikipedia.org/wiki/Shebang_(Unix)#Magic_number "#!" in ascii is the bytes 0x23 0x21 - when exec() sees these bytes the behavior is to treat the rest of the line as a path to an interpreter. Mar 27, 2018 at 19:31

It needs to be a comment because only this way it will also work to run a script like "interpretername scriptname". I do not know about the origin of the "!".

  • 1
    Even if you just run ./scriptname, the interpreter still sees the she-bang line, so it still needs to be a comment.
    – psusi
    Jul 13, 2011 at 18:18
  • 1
    Or to be more detailed: the shebang: '#!' is designed not to be seen by the interpreter -- thus it must start with the comment char '#'. Instead, it is 'seen' (and interpreted) by the kernel 'exec[lv]*' set of system calls.
    – arielf
    May 13, 2013 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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