11

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
  • 11,409

1 Answers1

13

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
  • 104,119