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.


I have few questions about the magic number #!, the shebang.
I read that it was introduced by Dennis Ritchie in the 70s as a way to see a script as an executable file.
I don't understand, why it was introduced. What brought him to introduce it? What did he like?
The second thing I don't understand is: why do I need to declare the script as an executable? I mean why do I have to do chmod +x myscript?
I think in the following way, once I put the string #! myinterpter then when I will make ./myscript the OS will take this file, it will see that it contains the shebang and then it will call the appropriate interpreter. So why do I need to use chmod +x? Why does the OS (unix, linux) need me to make my script executable?

share|improve this question

closed as off-topic by jasonwryan, Braiam, slm Oct 28 at 1:59

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
I can't comment about the history and purpose of #!, but chmod is granting permission to execute. I wouldn't say that chmod +x is actually "making it executable". I would categorize chmod +x as "allowing the file to be executed". You can do more fine tuning with permissions as well. chmod can tell the OS "only the owner of this file can execute it" or "users in the assigned group can execute this file". chmod +x is a short-hand (that I've actually never used, go figure). –  Marc Reside May 22 at 19:55
4  
Keep in mind you can have binary executables (for example, ELF binaries) that don't have the executable flag, the kernel doesn't know it can execute them. The executable bit tells the kernel to look at the first bytes to see what kind of executable it is. The shebang tells the kernel its a shell script. –  jsbillings May 22 at 20:08
6  
Everything you want to know about #! is at in-ulm.de/~mascheck/various/shebang –  Stéphane Chazelas May 22 at 20:13
1  
Everything you want to know about #! is in the Wikipedia article. –  Gilles May 22 at 23:16
1  
As to purpose, one purpose was to add a magic number to shell scripts. If you use the file or magic commands to determine what type of file is being examined (results could be piped through filter to find group), then the #! works as a two byte magic number. Try file /bin/* |grep shell as an example. –  bdowning May 23 at 15:26

Browse other questions tagged or ask your own question.