Operating System - HP-UX
1754098 Members
1045 Online
108811 Solutions
New Discussion юеВ

What does #!/bin/sh at the beginning of a file do?

 
SOLVED
Go to solution
Gary Cooper_1
Esteemed Contributor

What does #!/bin/sh at the beginning of a file do?

Am I right in thinking that:

#!/bin/sh
or
#!/bin/ksh

makes sure the script is run using the shell referenced in the first line? I.e. if the user is using ksh, it will over-ride ksh and execute the script as a sh script (or vice-versa).

Thanks,

Gary Cooper
8 REPLIES 8
Stefan Farrelly
Honored Contributor
Solution

Re: What does #!/bin/sh at the beginning of a file do?

ys, it confirms which shell to run the script under when executed.
Im from Palmerston North, New Zealand, but somehow ended up in London...
U.SivaKumar_2
Honored Contributor

Re: What does #!/bin/sh at the beginning of a file do?

Hi,

#! is a magic macro which at the beginning of a shell script will make the shell script executed by the specified shell.

eg:-

#!/bin/ksh
ls

the above shell script will use ksh shell to run ls command.

regards,
U.SivaKumar


Innovations are made when conventions are broken
Gary Cooper_1
Esteemed Contributor

Re: What does #!/bin/sh at the beginning of a file do?

Thanks guys for such a phenomenally rapid response - 5 minutes, wow!

The HP ITRC user forum really is great!

Regards,

Gary
H.Merijn Brand (procura
Honored Contributor

Re: What does #!/bin/sh at the beginning of a file do?

To be more correct, it does not have to be a shell. It can be any interpreter. Most of them will even take command line options in the `hashbang' line as it is called.

#!/usr/bin/perl -w

#!/usr/bin/sh

#!/usr/bin/awk

#!/home/user/scripts/myscript -x -duh

etc

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Frank Slootweg
Honored Contributor

Re: What does #!/bin/sh at the beginning of a file do?

In case you wonder where this is documented: In the exec(2) ("man 2 exec") manual page. Just search the page on "#!".
A. Clay Stephenson
Acclaimed Contributor

Re: What does #!/bin/sh at the beginning of a file do?

And lastly, it's pronounced
'shebang' so if you ever hear someone say it, it doesn't mean magic. Magic is altogether different and we have "magic numbers" and "magic cookies" and no I am not pulling your leg.
If it ain't broke, I can fix that.
Gary Cooper_1
Esteemed Contributor

Re: What does #!/bin/sh at the beginning of a file do?

And I thought Ricky Martin was singing about the wayward antics of some woman - I never guessed he was a Unix SysAdmin!

Cheers,

Gary
W.C. Epperson
Trusted Contributor

Re: What does #!/bin/sh at the beginning of a file do?

And from the old grey haired systems weenie....

It was orignally called "hashpling". See:
http://www.uni-ulm.de/~s_smasch/various/shebang/
"I have great faith in fools; self-confidence, my friends call it." --Poe