8

I just realized that in current Ubuntu version /bin/sh is no more symlinked to bash (as it used to be for many years), but to dash instead. I wonder what are the actual syntax differences between those two shells and how big is the probability that a shell script written with bash in mind won't work under dash. Can anybody point me to a good and clear description of differences between these two?

raj
  • 10,353

1 Answers1

8

A simple rule of thumb is: if your script was written in bash, do not assume it will work in dash. A full list of differences is beyond the scope of a simple Q&A, but essentially, dash is a POSIX shell, so it implements what is described in the POSIX specification for the shell language and only that.

Here are the common bashisms I most often fall afoul of:

  • [[: the [[ condition ]] construct isn't supported by dash, you need to use [ ] instead.
  • == : to test if two values are equal, use = in dash since == is not supported.
  • source: the POSIX command for sourcing a script is .. The source builtin is a bash alias to the standard ., so always use . file instead of source file.
  • shopt: this is a bash builtin that sets certain non-standard options. Not supported by dash.
  • $RANDOM: this is set to a random number on each use in bash, but doesn't work in dash.

By far the most common problem is the lack of [[ support. You can find a more comprehensive list on the Ubuntu Wiki: https://wiki.ubuntu.com/DashAsBinSh

terdon
  • 100,812
  • as a supplementation, 'function' is a keyword in bash to declare a function but not available in dash. check description here https://stackoverflow.com/questions/7917018/what-is-the-function-keyword-used-in-some-bash-scripts – Kevin Chan Jun 23 '22 at 02:01
  • Believe it or not, until today I've been Google searching for bourne shell instead of dash any time I'm trying to find supported syntax by /bin/sh – Sridhar Sarnobat Nov 04 '22 at 22:36