It is currently Tue, 07 Dec 2021 02:23:12 GMT

Author Message
 What is does '#!' do ?

This might be an easy question:
What purpose does '#!' serve in the call to a unix shell ?

All I have seen are '#!/bin/sh' '#!/bin/csh' and etc.

Can '#!' start the beginning of calls to other executables other than
the unix shells ?
Is it just for executables ?

Your replies are greatly appreciated.


 Mon, 21 Jul 2003 07:28:12 GMT   
 What is does '#!' do ?
Sharron D. Allen <> wrote:

 > What purpose does '#!' serve in the call to a unix shell ?

( That's the opportunity to find the 'unix-faq',
  which explains this in detail.  Posted also to this Newsgroup. )

Interesting what (POSIX/)SUSv2[1] say about the minimum portability:

 > If the first line of a file of shell commands starts with the
 > characters #!, the results are unspecified.
 > The construct #! is reserved for implementations wishing to
 > provide that extension. A portable application cannot use #!
 > as the first line of a shell script; it might not be interpreted
 > as a comment.

[1] <URL:>


 Mon, 21 Jul 2003 08:12:31 GMT   
 What is does '#!' do ?
In article <4e1e6.89$>,
  "Sharron D. Allen" <> wrote:

The shebang (as the '#!' is called) tells the shell running the script
what executable to use to process the commands in the script. In one of
your examples, '#!/bin/csh' indicates that this is a csh script. It
might use some features of csh that aren't available with other shells,
so it needs to be specified.

To be used this way the shebang has to be on the first line (& even the
first character of the line if I'm correct). You'll notice that some
shebang lines pass parameters as well, like '#!/usr/bin/perl -w' for

To answer your second question, it's only use is to mark excecutable
scripts. If you wrote a script without a shebang line then whatever
shell you were running at the time would be used to process the script.

THKS  %^>

Sent via

 Mon, 21 Jul 2003 08:25:01 GMT   
 What is does '#!' do ?
When the kernel is told to execute an executable file,
it checks the first two bytes. If the executable is a
compiled program, these "magic numbers" identify the
processor (Sparc, Pentium, PowerPC, etc.) the program is
compiled for. If these two bytes are "#!", the file is a
script and the rest of the line identifies the
interpreter to execute to run the script. You can
specify any interpreter you like, even one you write

 Mon, 21 Jul 2003 22:59:37 GMT   
 What is does '#!' do ?

    this last part of your post is incorrect. If you execute a script
with no shebang, /bin/sh is used to execute the script and NOT the
shell you are currently running.

    Try it out. Create a script called myscript that has this simple
csh statement:

setenv X test

$ chmod 755 myscript
$ csh
% ./myscript
./myscript: setenv:  not found.

You will get a syntax error from sh. Or, create a script that has a non-
Bourne shell specific syntax such as:


and then from ksh execute it. Again, you will get a syntax error
(assuming that /bin/sh is a true Bourne shell - this example fails on
HP-UX because /bin/sh there is a POSIX shell).

Anyway, its a common misunderstanding that people seem to have.


In article <95aack$>,
  Uncle Jeff <> wrote:

Sent via

 Tue, 22 Jul 2003 07:36:52 GMT   
 What is does '#!' do ?
In article <95cruk$>,

Interesting :-/. With all the scripts I've written so far, it has never
occurred to me to test this, and I've _always_ include the shebang.

Learn somthing everyday. Thanks for pointing that out, Kevin.

THKS  %^>

Sent via

 Tue, 22 Jul 2003 08:42:51 GMT   
 What is does '#!' do ?
In article <95cruk$>,

If exec finds no shebang,  it returns an error.  It is up to the shell
to decide what to do with it.  Bournish shells will fork and use the
file as input.  Csh will examine the first char - if it is a #, it
will fork and use the file as input,  otherwise it will use the
system() default shell to execute it.

So in your above example,  If you add a comment line above the setenv
csh will execute it.

Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 Tue, 22 Jul 2003 22:21:42 GMT   
 What is does '#!' do ?
On 2 Feb 2001 14:21:42 GMT, Dan Mercer <> wrote:

Occasionaly, you may see old shell scripts with a ':' on the first line.
This to avoid the csh to see a '#' in that position.  SCO xenix and SCO
unix used to have lots of those.


 Wed, 23 Jul 2003 01:28:30 GMT   
   [ 8 post ] 

Similar Threads

1. USYNC_PROCESS - what's am I doing wrong

2. Don't really know what I'm doing (fstab/mtab settings)

3. I've done everything, still can't mount linux dirs from Win95

4. Please help doing 'reget' with wget

5. Need FTP '% done' real-time gauge

6. Q: '' loop problem

7. Q: '' loop problem

8. [2.5] smp_init 'CPUS done' looks strange

9. Tomsrtbt = tomsnotbt - what'm I doing wrong?

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software