Friday, January 14, 2011

What does "#! /usr/bin/ksh" mean?

I had my own share of doubts while dealing with Unix scripting. Every time I forget what should be my beginning line. If it is #! /usr/bin/ksh, isn't '#' suppose to mean comment in unix scripting language? If so, then how does the kernel understands what shell I want this program to be executed?

Crawling through web, I stumbled upon this webpage where the user answer's my doubt THE BEST way.


For the convenience of single page reference for myself and all, here's what Perderabo has to say

This will probably be more than you wanted to know, but here goes anyway....

Originally, we only had one shell on unix. When you asked to run a command, the shell would attempt to invoke one of the exec() system calls on it. It the command was an executable, the exec would succeed and the command would run. If the exec() failed, the shell would not give up, instead it would try to interpet the command file as if it were a shell script.

Then unix got more shells and the situation became confused. Most folks would write scripts in one shell and type commands in another. And each shell had differing rules for feeding scripts to an interpreter.

This is when the "#! /" trick was invented. The idea was to let the kernel's exec() system calls succeed with shell scripts. When the kernel tries to exec() a file, it looks at the first 4 bytes which represent an integer called a magic number. This tells the kernel if it should try to run the file or not. So "#! /" was added to magic numbers that the kernel knows and it was extended to actually be able to run shell scripts by itself. But some people could not type "#! /", they kept leaving the space out. So the kernel was exended a bit again to allow "#!/" to work as a special 3 byte magic number.
So
#! /usr/bin/ksh
and
#!/usr/bin/ksh
now mean the same thing. I always use the former since at least some kernels might still exist that don't understand the latter.

And note that the first line is a signal to the kernel, and not to the shell. What happens now is that when shells try to run scripts via exec() they just succeed. And we never stumble on their various fallback schemes.

I am going to explore more on this magic number now and if you would like to add your thoughts to this, please do in comments below.

Happy Reading,

Regards
Amit