Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '14 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.

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 '14 at 19:55
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 '14 at 20:08
Everything you want to know about #! is at in-ulm.de/~mascheck/various/shebang – Stéphane Chazelas May 22 '14 at 20:13
Everything you want to know about #! is in the Wikipedia article. – Gilles May 22 '14 at 23:16
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. – bsd May 23 '14 at 15:26