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
  • 9,367
  • 2
  • 16
  • 42
  • Does this answer your question? [What is the point of sh being linked to dash?](https://askubuntu.com/questions/976485/what-is-the-point-of-sh-being-linked-to-dash) – Nmath Sep 26 '20 at 14:18
  • 1
    [DashAsBinSh](https://wiki.ubuntu.com/DashAsBinSh) is a good start; a more thorough reference for a POSIX compliant shell is [The Open Group Base Specifications Issue 6: Shell Command Language](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html) – steeldriver Sep 26 '20 at 14:24
  • @Nmath: None of the answers there does clearly specify differences between those shells. What I would like is some form of a detailed list of `bash` commands or other syntax features that are not supported in `dash`. – raj Sep 26 '20 at 14:25
  • Don't know about dash, but at least the root user should initially have a shell with very minimal requirements to hardware and software. In case of emergency, a *fat* shell might not be able to even start up. – Gyro Gearloose Sep 26 '20 at 14:30
  • how about https://lwn.net/Articles/343924/ ? – Rinzwind Sep 26 '20 at 14:47
  • Don't know dash, but use fish and most bash commands work. Most differences are when combining commands with &&. Probably a few others. – crip659 Sep 26 '20 at 15:17
  • FYI if you want to test a particular script for sh/POSIX compatibility, the `devscripts` package provides a `checkbashisms` script for that – steeldriver Sep 26 '20 at 17:18
  • 1
    Does this answer your question? [What is the difference between #!/bin/sh and #!/bin/bash?](https://askubuntu.com/questions/141928/what-is-the-difference-between-bin-sh-and-bin-bash) – KK Patel Oct 11 '20 at 07:10
  • @KK Patel the link you specified is totally unrelated to my question. I obviously understand that bash and dash are two different shells. I was asking about the exact set of syntax differences. – raj Oct 11 '20 at 22:02

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
  • 98,183
  • 15
  • 197
  • 293
  • 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