When sh is a symlink to bash or dash, bash limits itself to POSIX-compliance, so it should be 100% compatible with sh?

From Difference between bash and sh:

ABck to the question: If you have /bin/sh as a link to bash, then bash will not behave the same when called as /bin/sh as it does when called as /bin/bash. When called as sh, it will limit itself to mostly POSIX-compliance plus a limited set of extensions.

Does it mean that whenever I come across a shell script in Linux with a shebang to sh: #!/bin/sh, even if on that distro, bin/sh is a symlink to another shell, like dash or bash, it should be 100% compatible with the bourne shell, since it limit itself to a limited set of extension? So I could execute them in FreeBSD? Is there are exception to that? Or I should be safe to assume that it will work?

So if on a distro, bin/sh is a symlink to bin/bash, and a script use #!/bin/sh and the script contain bashism, it will not run, since bash will like be in sh mode?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

No, if /bin/sh is a symlink to bash bash enters just posix mode – from man bash:

When invoked as sh, bash enters posix mode after the startup files are read.

If you now search for posix mode at the bash manpage you’ll see that some shell builtins like time or source behave differently. That is all. bashishms like writing function before declaring a function or using source instead of . still will work but the commands may behave differently.

So if /bin/sh is a symlink to /bin/bash all typical bashishms still work.

For a pretty extensive list of differences in the Posix mode have a look at the bash reference manual.

Method 2

There seems to be some confusion around the Bourne shell. The Bourne shell was Unix shell decades ago, in the pre-POSIX days. Nowadays “sh” is an implementation or another of a shell that implements the POSIX specification, among which we have bash, pdksh, AT&T ksh, newer Almquist shells and their derivatives that have been made POSIX compliant (including the “sh” of some BSDs, other BSDs sh being based on pdksh and Debian ash (dash)). Even zsh has a mode in which it is mostly POSIX conformant.

The Bourne shell is not POSIX compliant, and can’t be found on many system nowadays. Where the Bourne shell is found or where it’s not, there will always be a “sh” that is POSIX compliant (and will not be the Bourne shell), generally in /bin while a notable (annoying) exception is Solaris.

Note that before the Bourne shell, and we’re talking over 30 years ago, “sh” was the Thompson shell. We’re not writing scripts compatible with the Thompson shell anymore. Similarly, we should stop thinking of “sh” as the Bourne shell.

Now “sh” is a specification, not many sometime incompatible variants of an implementation. That makes it a lot easier to write portable scripts. Just follow the specification.

That goes for “sh” and all the standard utilities (sed, cut, tr…)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x