As there are different kinds of shells, What is the difference between bash and shell scripts and is there any compatible script on can write that runs on al unix shells

  • 1
    Have a look at the bash tag wiki. – phk Oct 16 '15 at 12:38

A bash script is a script interpreted by the Bourne-Again SHell and is therefore a shell script. It may or may not be executed successfully by other shells. If you use bash-specific syntax, you cannot be sure that it will run with other shells.

If you want to write a script that can be interpreted by every shell, you have to use the syntax described in the POSIX standard at Shell Command Language. Of course, not ALL shells honour the POSIX standard, but that's the best you can do. Such scripts can be very portable and can run on many Linux and *nixes in the same way without throwing errors.

Many shells and command line tools look for an environment variable called $POSIXLY_CORRECT. If that is set, those tools behave like a posix-conformant tool would behave.

Another attempt is the --posix flag. For example GNU sed has a flag called --posix which disables all GNU extensions and sed behaves similar to a posix sed.

bash supports both --posix and the $POSIXLY_CORRECT variable.

If you write your scripts in that manner and test them with posix-compatible shells, you're on the best way.

| improve this answer | |
  • There is just a small problem: there is no shell implementation that is POSIX compatible with no extensions. So the test you propose is not useful. ALso bash is not POSIX compatible enough to be used as a gauge. – schily Oct 16 '15 at 11:43
  • 1
    @schily If there is no shell implementation that is POSIX compatible with no extensions, the test in your answer wouldn't be useful too. Or why is that shell of your own better than any other? – chaos Oct 16 '15 at 11:53
  • You seem to missinterpret things. Bash is very non-standard with respect to POSIX, there are many nonstandard features that you cannot disable. The closest POSIX implementation is a special version of ksh88 but this still implements non-POSIX extensions (such as '[[') and this shell is closed source. I do not recommend to go POSIX but to go below what POSIX defines and the shell I offered is just the original SVr4 shell source (from Solaris) made portable with the help from Geoff Collyer (the same serson that helped David Korn to make ksh portable). – schily Oct 16 '15 at 12:08

Bash is a mixture of a clone of the features from Bourne Shell and ksh88 (partly also ksh93).

Shell today is a too unprecise term...

Bash scripts are scripts that make use of vendor unique features of the bash implementation.

If you like to write portable scripts, there are two methods:

1) write scripts to conform to the POSIX standard, but then you need to know how to call a POSIX shell on all platforms. Note that you will not get a POSIX shell by calling /bin/sh. You would rather need to set your PATH to the output of getconf PATH an then call sh without using a PATH - you see this is not useful for installed scripts...

2) write scripts that are Bourne Shell compatible. These scripts will work with nearly any (basically Bourne Shell based) shell. So limit your self to the minimum set of features and assumptions on constraints.

If you like to check your script for portability, I recommend you to check the script with the SYSVr4 Bourne Shell implementation. A portable version of the SVr4 Bourne Shell is in the Schily Tools tarball:


Compile and check for the binary called osh. This is the portable SVr4 Bourne Shell.

| improve this answer | |

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