Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
/bin/sh is usually (always, as far as I know ... but I could be wrong) a link to another file. Using Slackware, I know it is a link to /bin/bash, using Ubuntu 9.04 it is a link to /bin/dash (the Debian Almquist shell).
"sh" is the name of the original Bourne Shell. Nowadays, most shells try to be backwards compatible with it, so, at least theoretically, most actual shells should be able to run an old script written for "sh" without needing any modification at all. "sh" is old and not too featureful, so nowadays linux distros use anything else (bash is very famous, but other common ones are ksh, csh, zsh, dash, ash...).
/bin/sh is usually a symlink to any of these (I am not sure all of them could comply, particularly csh), so if in your system /bin/sh is a symlink to /bin/bash, there's no difference in using either of them. However, if you aim for the widest portability, you want to use #!/bin/sh in your scripts. Of course, that means that you then shouldn't use any bash-only feature (bash is a lot more complete than the original sh). All the unix alikes will always have a substitute for sh, but not all of them will always have bash installed, or any other shell by that matter.
That's why the #!/bin/sh bang is still used, and will continue to be so.
if in your system /bin/sh is a symlink to /bin/bash, there's no difference in using either of them.
Well, not quite. Although /bin/sh is a symlink to /bin/bash, when bash is called as /bin/sh, its behavior is somewhat different than when it's invoked as /bin/bash. It's right there in man bash:
Quote:
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.
But otherwise you're correct, /bin/sh is added for backward compatibility and maximum portability.
You are quite right, in my aim to simplify the issue I omitted info so that part of my post wasn't totally correct. However, even when invoked that way, all the bashishms will still work, you can try this one which contains bash specific code that would fail to run on the genuine bourne sh
I've read this was true, also, but thought saying it was a link or symlink would be easier, and I haven't really read about the details of using sh instead of bash and what differences it makes. I do remember reading that it becomes POSIX, but not fully from what I remember.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.