Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-15-2010, 06:54 AM   #1
Registered: Mar 2010
Location: California
Distribution: Slackware
Posts: 235

Rep: Reputation: 18
The Significance of #!/bin/sh at start of script?

Hi All,

What does #! /bin/sh mean in a shell script? Is it mandatory to include in a shell script? I'm able to execute the shell script without it.

Any help on this would be appreciated.
Old 03-15-2010, 06:59 AM   #2
LQ Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,220

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683

You might take a look at this

Old 03-15-2010, 07:08 AM   #3
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Well, consider these scripts:
jc@jcmain:~/scripts$ cat 
#!/usr/bin/env bash
echo "hello world"
jc@jcmain:~/scripts$ cat 
echo "hello world"
One Has the shabang on the top, the other doesn't. When I run them both as per:
jc@jcmain:~/scripts$ bash 
hello world
jc@jcmain:~/scripts$ bash 
hello world
They both output the same; however if I run the file command on the two of them:
jc@jcmain:~/scripts$ file a bash script text executable
jc@jcmain:~/scripts$ file ASCII text
We see that they're both different. This is because when you execute the scripts they check the top line to see what interpreter to run the code in. Now yes, both of those will run as ./<script> because they both contain a bash command, but suppose I have a file as:
jc@jcmain:~/scripts$ cat
import time
print time.asctime()
and try the same? Well, we know it is a python script, and passing it to python is fair enough:
jc@jcmain:~/scripts$ python 
Mon Mar 15 12:05:33 2010
But what happens if we make it executable?
jc@jcmain:~/scripts$ chmod +x 
jc@jcmain:~/scripts$ ./ 
./ line 1: import: command not found
./ line 2: syntax error near unexpected token `('
./ line 2: `print time.asctime()'
Because the shabang is missing it doesn't know how to interpret it.

So basically, we put the shabang in because when we make it executable, for ease of use, we need to tell it how to run. If that code had a shabang in, then it works perfectly

jc@jcmain:~/scripts$ cat
#!/usr/bin/env python
import time
print time.asctime()
jc@jcmain:~/scripts$ chmod +x 
jc@jcmain:~/scripts$ ./ 
Mon Mar 15 12:08:06 2010
And so on.

Make sense? Or did I explain that badly?
1 members found this post helpful.
Old 03-15-2010, 07:58 AM   #4
Senior Member
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Nicely put. Maybe put in that most of the time bash is the standard shell on linux and thus every text file when run is interpreted by bash?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] #!/bin/sh vs #!/bin/bash -- script executes differently; each way = different bugs GrapefruiTgirl Programming 21 12-16-2009 05:30 PM
start script bash when system start,stop,reeboot, how ?? melmar Linux - General 4 12-10-2009 06:58 AM
echo $PATH = /home/g3rc4n/bin:/usr/local/bin:/usr/bin:/bin:/usr/games ? i_heart_pandas Linux - Software 7 09-18-2009 08:33 AM
significance of $$ in a logcheck script? Kjell Persson Linux - Newbie 2 03-18-2006 09:50 PM
openoffice won't start. cannot execute bin. y? dark_prancer Linux - Software 3 10-15-2005 06:55 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration