Tell me more ×
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
3  
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
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

2 Answers

up vote 9 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
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

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
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 at 20:12

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.