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
#!/bin/echo Hello
echo world
jimmy@vera ~/code/bash $ ./blah.sh
Hello ./blah.sh
I can understand if it were to execute either the second line (with the first ignored as a comment), or the second (and stopping after running echo). But it echo's both Hello and the filename...
Similarly with cat:
jimmy@vera ~/code/bash $ cat me
this file is called me
jimmy@vera ~/code/bash $ cat blah.sh
#! /bin/cat me
echo Hello world
jimmy@vera ~/code/bash $ ./blah.sh
this file is called me
#! /bin/cat me
echo Hello world
Again, it executes the first line (with the #!), but not the second, and again it adds itself as a parameter to the first line.
and to be neat and tidy, I tried rm as well:
jimmy@vera ~/code/bash $ cat blah.sh
#! /bin/rm me
echo hello world
jimmy@vera ~/code/bash $ ls
blah.sh filenamesWithSpaces.sh me
jimmy@vera ~/code/bash $ ./blah.sh
jimmy@vera ~/code/bash $ ls
filenamesWithSpaces.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?