Register
It is currently Wed Apr 23, 2014 1:33 pm

Difference between /bin/bash & /bin/sh


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sat Dec 27, 2008 5:03 am   

Joined: Sat Mar 22, 2008 11:51 pm
Posts: 5
Location: Nagpur, Maharasthra, India
I know it's a very silly question but could someone please explain the difference between "/bin/bash" & "/bin/sh"

I was under the impression that both are same but following output on my Ubuntu 8.10 is making me raise my eyebrows.

Quote:
parag@station3:~$ ls -l /bin/bash ; ls -l /bin/sh
-rwxr-xr-x 1 root root 725136 2008-05-13 00:18 /bin/bash
lrwxrwxrwx 1 root root 4 2008-12-03 21:42 /bin/sh -> dash
parag@station3:~$


Top
 Profile WWW YIM  
 PostPosted: Sat Dec 27, 2008 6:14 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
The diffrence between shells is just what they're capable of doing.
Bash is newer and more extensive in functionality then sh.

Can't tell you the exact things that set them apart but the manpages might give you a hint.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Sat Dec 27, 2008 6:18 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
If you use

#!/bin/bash

at the head of your script your going to make sure your running in the BASH shell.

If you use

#!/bin/sh

your going to use WHATEVER shell is linked to sh. This is "normally" bash in most distro's....... Ubuntu in their "infinate" wisdom, thinks that linking sh to /bin/dash (the dash shell) is the way to go. I never use dash myself, I've always used bash or ksh. Simply putting the correct shebang line at the top of your scripts will solve any issues with regards to what your script is going to run in.

You can modify your Ubuntu system's /bin/sh symlink to point to /bin/bash:
Code:
# cd /bin
# sudo rm sh; ln -s bash sh


Top
 Profile WWW  
 PostPosted: Sat Dec 27, 2008 11:31 am   

Joined: Sat Mar 22, 2008 11:51 pm
Posts: 5
Location: Nagpur, Maharasthra, India
Thanks guys...I got my answer...so the conclusion is:

1. If am executing the shell script using absolute or relative path, then interpreter used would be the one mentioned as sha-bang header.
2. And if I am using "sh" to invoke the shell script, interpreter used would be the one linked with "/bin/sh"


Top
 Profile WWW YIM  
 PostPosted: Sat Dec 27, 2008 9:50 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
Correct.


Top
 Profile WWW  
 PostPosted: Sun Dec 28, 2008 2:06 am   

Joined: Sat Mar 22, 2008 11:51 pm
Posts: 5
Location: Nagpur, Maharasthra, India
Just to add more info, 3rd way to execute a script is directly through the interpreter. For e.g. consider following python script

#cat /tmp/sha_python
Quote:
if 1==1:
print "Equal"
else:
print "No"


I can execute it using:
#python /tmp/sha_python
Quote:
Equal


The interesting thing to note that is if I add a sha-bang header to it. i.e.

#cat /tmp/sha_bang_python
Quote:
#!/usr/bin/python
if 1==1:
print "Equal"
else:
print "No"


and if I invoke it using a wrong interpreter say "perl" still the script would work:
#perl /tmp/sha_bang_python
Quote:
Equal


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP