![]() |
What does the ! do in #! at the start of a .sh?
Someone mentioned to me recently that the ! part of the #! to start a shell script had some special meaning. He mentioned something along the lines of how the # denotes a comment, but the ! causes the line to still be interpreted somehow. So I was playing around with it and noticed some odd things. It seems like the script calls itself, but with itself as a parameter? Or does the #! line just prepend itself to the list of parameters actually passed in (hence how a script gets itself as $0)?
First some oddities with echo: :
jimmy@vera ~/code/bash $ cat blah.sh Similarly with cat: :
jimmy@vera ~/code/bash $ cat meand to be neat and tidy, I tried rm as well: :
jimmy@vera ~/code/bash $ cat blah.sh The second guess I came up with seems almost logical, except that at this point I'd modify it to say it somehow passes the file and its arguments to whatever is cited after the #!. But I'm still making guesses, so does anyone know how it actually works? |
It's called a shebang. It's the *nix way of determining what interpreter to use when executing a shell script. The operating system looks for the shebang at the start of the script its executing, and then passes the entire contents of the file to the program specified by the shebang. Any additional arguments are appended to the end of the program.
For instance, say I have a file called hello.fb: :
#! /usr/bin/foobar -y:
./hello.fb world:
/usr/bin/foobar -y world < hello.fb |
Now for the useless information: shebang is a corruption of hash bang, the # being the hash mark and the ! being the bang mark.
|
It constitutes a magic number. UNIX can interpret the magic number for a lot of files, and then behave accordingly.
#!/bin/ksh loads the ksh shell for the duration of the script - the script file becomes stdin. look for /usr/include/magic.h and /usr/include/sys/magic.h for more information. man magic or info magic may also help. |
I've heard a few UK-based Unix hackers call it the "Whiz bang." Not really very informative, I know, but I find it quite amusing. Maybe I should get out more...
|
| All times are GMT -5. The time now is 9:17 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC