What is the difference between bash script and shell script
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.
Related videos on Youtube
JohnTheBeloved
Updated on September 18, 2022Comments
-
JohnTheBeloved over 1 year
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
-
phk over 8 yearsHave a look at the bash tag wiki.
-
-
schily over 8 yearsThere 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. -
chaos over 8 years@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?
-
schily over 8 yearsYou 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).