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?
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 bydash
, 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.
. Thesource
builtin is a bash alias to the standard.
, so always use. file
instead ofsource file
.shopt
: this is a bash builtin that sets certain non-standard options. Not supported bydash
.$RANDOM
: this is set to a random number on each use inbash
, but doesn't work indash
.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