Aim of this section: Ensure that your script is run with a compatible interpreter.

Portability is a bit of a sore subject in shell scripting. On the one hand, using portable language features means that the code is more likely to do the expected actions when run with different interpreters (or different versions of the same interpreter), which can save users time. On the other hand:

  • It is not possible to write a fully portable script, because there are a huge number of interpreters and versions, and the target interpreters are (pretty much by definition) unknown

  • The script could do something subtly wrong when run by another interpreter, like doing nothing when it should be doing something

  • The script could do something catastrophically wrong when run by another interpreter, like deleting all the user’s files

  • A lot of useful tools and Bash–specific features are not portable, so portable code will be more complex than non–portable code with the same feature set

  • When something goes wrong the user is going to have a nasty time trying to debug complex code written by someone else for a most likely unknown set of interpreters and versions


This page is a preview of The newline Guide to Bash Scripting

Start a new discussion. All notification go to the author.