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?
- https://git.kernel.org/pub/scm/utils/dash/dash.git/commit/?id=48ca00863af909461d1372998bb90549f27abaaf
- https://git.kernel.org/pub/scm/utils/dash/dash.git/commit/?id=a29e9a1738a4e7040211842f3f3d90e172fa58ce
- https://git.kernel.org/pub/scm/utils/dash/dash.git/commit/?id=878514712c5d21f675c45d99d2f8a04098ea4a19
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)}