Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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). –  Gilles Jul 13 '11 at 13:08
    
Missed to check wikipedia :) –  Johan Jul 13 '11 at 13:30
1  
I just wish the shells were smart enough to strip an extraneous CR/LF if it is there... ;) –  Aaron D. Marasco Jul 14 '11 at 1:02
add comment

3 Answers

up vote 16 down vote accepted

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

share|improve this answer
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 '11 at 11:32
add comment

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.

share|improve this answer
    
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 '13 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 '13 at 15:28
add comment

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 "!".

share|improve this answer
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 '11 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 '13 at 20:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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