Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 16th, 2006, 3:47 AM   #1
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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 
#!/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?
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Nov 16th, 2006, 4:01 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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
All of this text as well as the line above
is passed to "foobar -y" as STDIN
Then this line:
./hello.fb world
Is the same as:
/usr/bin/foobar -y world < hello.fb
Arevos is offline   Reply With Quote
Old Nov 16th, 2006, 7:08 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Now for the useless information: shebang is a corruption of hash bang, the # being the hash mark and the ! being the bang mark.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 16th, 2006, 5:27 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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.
jim mcnamara is offline   Reply With Quote
Old Dec 1st, 2006, 5:47 PM   #5
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4 mackenga is on a distinguished road
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...
__________________
"I'm not a genius. Why do I have to suffer?"
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I am very novice and have no idea where to start mjmawby Community Introductions 25 Mar 15th, 2006 11:57 AM
Ok - so how do i start the proj?? k4pil Project Ideas 9 Aug 30th, 2005 2:44 PM
best language to start with bl00dninja Coder's Corner Lounge 5 Apr 25th, 2005 2:41 AM
C/C++ Which to start with? Fred C++ 18 Apr 19th, 2005 1:10 PM
Where to start? brightboy90 Coder's Corner Lounge 2 Feb 2nd, 2005 11:14 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC