In using i see 2 types of code

#!/usr/bin/sh

and

#!/user/bin/bash

I have Googled this and the opinions vary a lot. The explanation I have seen on most websites is that sh is older than bash, and that there is no real difference.

Does someone know the difference between these and can give a practical example when to use either one of them.

I highly doubt that there is no real difference, because then having to things that do the exact same thing would be just

link|improve this question

4  
Did you mean to accidentally the end of that last sentence? – njd Mar 30 '10 at 16:18
1  
@njd: Well to be honest the way i ended the last sentence at the end was – Saif Bechan Mar 30 '10 at 16:43
lolwut? Somebody explain why Saif Bechan doesn't finish his – marcusw Mar 30 '10 at 17:19
1  
It's probably some kind of bug in – Kevin Panko Mar 30 '10 at 17:29
1  
Actually, it's pretty surprising. I've looked through the JS code for the editor and found a bug in – Rich Bradshaw Apr 22 '10 at 13:18
feedback

6 Answers

up vote 21 down vote accepted

bash is a superset of sh ie. everything you can do in sh you can do in bash.

Bash has more features (branching, builtins, arrays) making script easier to write. Some later *nix'es have /bin/sh as a link to /bin/bash

For a full explanation of what here's a tutorial

link|improve this answer
3  
@Saif Bechan: One reason that the Bourne shell (sh) was not just extended is that Bash` was written by someone else. Also I bet there were license issues. Read the article about the Bourne Shell en.wikipedia.org/wiki/Bourne_shell – Felix Mar 30 '10 at 16:32
6  
Removing sh would break a lot of scripts which expect it to be there and rely on the way it parses things. Linux users might not care, but people spending $$thousands on Solaris, AIX or HP-UX might be very annoyed. – njd Mar 30 '10 at 16:45
3  
...and for even more fun, Ubuntu symlinks /bin/sh to dash. Dash isn't completely sh or bash compliant, but is supposed to start up faster. When the system is booting, all the init.d scripts run and I guess the time saved overall is worth it. – kbyrd Mar 30 '10 at 17:13
6  
I'm pretty sure Dash is completely sh compliant. The problem is that some people write scripts that say /bin/sh, but the script itself requires /bin/bash to work. Nobody notices a problem, because most of the time, /bin/sh simply points at /bin/bash. – davr Mar 31 '10 at 22:53
4  
@Saif: There is another reason why sh was not simply extended: Its source code is pure hell. Take a look. It's supposed to be C... – grawity Mar 21 '11 at 9:20
show 8 more comments
feedback

Traditionally, /bin/sh would have been the original Bourne shell, which has no history or command-line editing, and no job control.

For about the last 15 years or so, most Unixes have had the POSIX shell installed, or at least ksh or bash (which are very nearly POSIX-like), but still have the more limited shell in /bin/sh

The reason for that is so that older shell scripts which expect the older sh command will still work.
Since characters like {, } and ! have special meaning to bash, it's possible that an older shell script using those characters (without escaping them) could fail.
(The Bourne shell would take !!{1,2} literally, whereas bash would interpret that as a repeat of the previous command (!!) followed by a brace-expansion).

On Linux though, the sh command is almost always just a link to bash, with all the same features.

link|improve this answer
2  
Bash is supposed to (but doesn't always) behave in sh-compatibility mode if invoked as /bin/sh. A lot of stuff broke when Ubuntu switched /bin/sh from linking to /bin/bash to linking to /bin/dash. The stuff that broke assumed bashisms when they should have been using standard sh. – Broam Mar 31 '10 at 22:07
feedback

Actually, even though /bin/sh may be a link to /bin/bash, if started up as sh it behaves differently. From the bash manpage:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

So as sh, it tries to emulate historical sh behavior. As bash, it tries to be as useful as possible as an interactive login shell.

link|improve this answer
feedback

for a general overview over the different shells (not just sh and bash):

http://en.wikipedia.org/wiki/Comparison_of_command_shells

link|improve this answer
But that comparison doesn't list 'sh'. – njd Mar 30 '10 at 16:19
lol its always funny when people respond with a wikipedia page. Thanks for the effort anyways. – Saif Bechan Mar 30 '10 at 16:28
@njd: sh is mostly just a link to bash, if not then to ash. – akira Mar 30 '10 at 20:06
feedback

Another great history here: http://www.faqs.org/faqs/unix-faq/shell/shell-differences/

link|improve this answer
feedback

On many systems and on Solaris in particular, bash is dynamically linked while sh is statically linked. This may pose a security threat, for this reason root user should only use /bin/sh as shell (if you ever need to log as root).

link|improve this answer
Also may be useful in an emergency if you did something bad with ldconfig or your /lib directory is wiped out for some reason. – ultrasawblade Mar 6 at 17:56
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.