What is the difference between bash script and shell script

5,556

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.

Share:
5,556

Related videos on Youtube

JohnTheBeloved
Author by

JohnTheBeloved

Updated on September 18, 2022

Comments

  • JohnTheBeloved
    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
      phk over 8 years
      Have a look at the bash tag wiki.
  • schily
    schily over 8 years
    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.
  • chaos
    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
    schily over 8 years
    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).