Skip to content

dash bug with string expansion and assignment of default values

Noticed when compiling OpenFOAM-v2006 modules with ubuntu bionic (18.04) that the module prefix is not being correctly set. Seems to be a weird bug in the dash shell expansions.

Starting from any non-root directory

echo "$PWD"
-> /home/user/openfoam/BuildEnv/scratch

dash 0.5.8-2.10 (bionic) shows this

unset test1
echo "${test1:=${PWD%/*}}"
-> /home/user/openfoam/BuildEnv/scratch  <-- WHAT?? Not what we expected

The additional quotes cause incorrect expansion. Whereas

unset test1
echo ${test1:=${PWD%/*}}
-> /home/user/openfoam/BuildEnv  <-- Expected
  • bash and dash 0.5.10.2-6 (focal) work as expected, with/without quotes
  • quoted expansion with :- works as expected.

Possible fixes?

Possible workarounds

  • use unqoted version, but risk fixing it on the next shellcheck
  • explicit if [ -z VAR ]; then ...; fi construct
  • with dirname as : "${VAR:=$(dirname xxx)}