Different result from $((++n)) when running bash vs dash Different result from $((++n)) when running bash vs dash shell shell

Different result from $((++n)) when running bash vs dash


dash is the Debian Almquist shell and an extreme light-weight version of a full POSIX-compliant shell-implementation of /bin/sh that aims to be as small as possible creating faster bootup times.

Operators such as $((n++)), $((--n)) and similar are features that are not required by POSIX and therefore not implemented.

To see how dash interprets these statements, see Chepner's answer

A nice page explaining how to make your script POSIX compliant, is here.


2.6.4 Arithmetic Expansion: Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format for arithmetic expansion shall be as follows:

$((expression))

The expression shall be treated as if it were in double-quotes, except that a double-quote inside the expression is not treated specially. The shell shall expand all tokens in the expression for parameter expansion, command substitution, and quote removal.

Next, the shell shall treat this as an arithmetic expression and substitute the value of the expression. The arithmetic expression shall be processed according to the rules given in Arithmetic Precision and Operations, with the following exceptions:

  • Only signed long integer arithmetic is required.
  • Only the decimal-constant, octal-constant, and hexadecimal-constant constants specified in the ISO C standard, Section 6.4.4.1 are required to be recognized as constants.
  • The sizeof() operator and the prefix and postfix ++ and -- operators are not required.
  • Selection, iteration, and jump statements are not supported.

source: POSIX IEEE Std 1003.1-2017


Prefix ++ is not required by POSIX, and dash doesn't implement it. Instead, it's parsed as two unary + operators:

$ n=1$ echo $((+(+n)))1$ echo $((++n))1$ echo $n1