LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-15-2010, 06:54 AM   #1
linuxunix
Member
 
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
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

You might take a look at this

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

Rep: Reputation: 70
Well, consider these scripts:
Code:
jc@jcmain:~/scripts$ cat small_script_one.sh 
#!/usr/bin/env bash
echo "hello world"
jc@jcmain:~/scripts$ cat small_script_two.sh 
echo "hello world"
One Has the shabang on the top, the other doesn't. When I run them both as per:
Code:
jc@jcmain:~/scripts$ bash small_script_one.sh 
hello world
jc@jcmain:~/scripts$ bash small_script_two.sh 
hello world
They both output the same; however if I run the file command on the two of them:
Code:
jc@jcmain:~/scripts$ file small_script_one.sh 
small_script_one.sh: a bash script text executable
jc@jcmain:~/scripts$ file small_script_two.sh 
small_script_two.sh: 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:
Code:
jc@jcmain:~/scripts$ cat small_script.py
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:
Code:
jc@jcmain:~/scripts$ python small_script.py 
Mon Mar 15 12:05:33 2010
But what happens if we make it executable?
Code:
jc@jcmain:~/scripts$ chmod +x small_script.py 
jc@jcmain:~/scripts$ ./small_script.py 
./small_script.py: line 1: import: command not found
./small_script.py: line 2: syntax error near unexpected token `('
./small_script.py: 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

Code:
jc@jcmain:~/scripts$ cat small_script.py
#!/usr/bin/env python
import time
print time.asctime()
jc@jcmain:~/scripts$ chmod +x small_script.py 
jc@jcmain:~/scripts$ ./small_script.py 
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
zhjim
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?
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:43 PM.

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