Bash – When is it important to write portable scripts

bashportabilityscriptingshell

Most code I write is in PHP. I have recently started learning shell scripting. Most of the resources and tutorials that I've come across are specific to Bash. Some warn about bashisms and some don't. I've been reading a lot on here and Stack Overflow.

Whenever an answer uses bashisms, someone will inevitably comment to say:

You shouldn't use <insert bashism here>. It's not portable.

This happens even when the question was tagged with bash. To me, that's like telling a PHP programmer that they shouldn't use code that's new in PHP 5 because it can't be used with PHP 4. Or telling someone they shouldn't write something for Mac because it can't be used on Windows.

When I write in PHP, I pick a minimum requirement and I write forward-compatible code. I don't worry about making it backward-compatible.

If I use #!/bin/bash as the shebang, why shouldn't I use bashisms? I'm starting to get the impression that some people just like to bash bashisms (pun intended) just for the sake of it.

People often use bash and shell interchangeably – probably due to the fact that bash is the default shell on many systems. So I can understand adding a comment to warn that the code uses bashisms, but I don't understand the implication that it's wrong to use them.

Obviously, if I am writing a script strictly for personal use I can write it in whatever language I want. But I'd like to think some of the code I write could be useful to others.

I tried searching for an answer to my question before posting. I found a lot of information about how to test for portability, but couldn't find anything about when it's important to do so.

So, when is it important to write portable scripts?

For example,

  • what types of scripts should be as portable as possible?
  • how common are systems that do not have Bash installed?
  • if the system has Bash installed, will it also have the GNU version of find and other utilities?

Best Answer

As a member of this community I do not often see people disregarding bashisms for things that target bash. When talking about portability, GNUisms are much worse than bashisms. As long as you use bash for your shebang, you can expect the script to be executed using bash. However, you can't guarantee the version unless you explicitly check. Bash version 4 brought some useful features such as associative arrays, but Bash v3 is still widely used on OS X and RHEL 5.

what types of scripts should be as portable as possible?

This is more about your needs and what you choose to support as the developer. If you are writing an install script for an application that supports all types of *NIX, then portability will be very important.

how common are systems that do not have Bash installed?

FreeBSD and Solaris 10 are examples of systems that do not come with bash installed by default. Most Linux systems will have it, with the exception of embedded systems.

if the system has Bash installed, will it also have the GNU version of find and other utilities?

No, and this is the real issue with portability. If portability is important, you should only use shell command features that are defined by the POSIX standard. GNU utilities can be installed on non-GNU systems such as FreeBSD, but they are unlikely to be first in PATH, and you can not rely on those tools to be installed.