Question

I usually write my scripts in bash. I tell other people to run them in bash, and I use #!/usr/bin/env bash as the shebang line.

When I read about shell scripting I often find comments with suggestions of "portable" solutions that do not use "bashisms". A portable shell script seems to be a one that can be run in any Bourne Shell derivative, eg. bash, ksh, or csh.

To me this seems a bit weird. Why would you not know what shell you run your script in? So my question is: What is the actual use of portable shell scripts? If you feel like using bash features, why not simply use bash?

I assume that there are common situations when portability is required, for why would it otherwise be mentioned so often? Perhaps certain environments require portability for historical reasons - I don't know - but I'm interested in finding out what these environments are.

Thanks in advance!

EDIT:

My conclusions from your answers and comments (thank you!):

  • You may need to avoid newer bash features because they may not be available in all bash versions, and some computers will have old versions installed.
  • An installer script should be runnable on computers you don't control. Not all Unix-like OSes come with bash by default. On the contrary the POSIX shell is nearly universally available, therefore it provides better reach than bash.
  • You may need to run a script on 1000 computers, but you don't want to (or can't) install bash everywhere. A POSIX shell is probably available.
  • You may not even be able to install bash on the computer you like. For example this might be due to lack of administration rights, the lack of a bash package for the platform, or the lack of resources like in a "bare-bones" computer. Again, a POSIX shell is probably available.
  • In some environments you can't assume the shell to be anything more than a POSIX shell, for example in System V style init scripts. (Is this correct?)

Bottom line: You can't always use bash anywhere you like, but a POSIX shell is likely already there.

Was it helpful?

Solution

Why would you not know what shell you run your script in?

  • You are writing an installer script that must run on an unknown machine.
  • You are writing a single script that will work with multiple known shells.
  • Not all bash features work the same even across different versions of bash (see the compat31, compat32, compat40, compat41 options).

Some common examples of non-bash usage:

  • dash is the system shell for Debian and Ubuntu
  • Mac OS X continues to ship with bash 3.2 instead of 4.x
  • Solaris uses, I believe, a version of ksh
  • bash needs to be installed separately on FreeBSD systems.

OTHER TIPS

This would be for Bourne Shell compatibility itself, so that the script can run on a bare-bones machine that simply has only sh or any other machine that has any (Bourne compatible) shell(s). You'd obviously only do this for full and complete compatibility.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top