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
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
bash supports both
--posix and the
If you write your scripts in that manner and test them with posix-compatible shells, you're on the best way.
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
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.