# Demonstration of a parameter substition bug in Dash
#       Mark Lentczner
#       mark@glyphic.com
#       January 2013

echo This tests certain parameter substitutions when both bare and quoted
echo In all cases, the values should be the same.
echo
echo In the test, a parameter is set to \"abc\", and then the % substitution is
echo used to remove a matching tail. The pattern to match will always be \"bc\"
echo but it will provided several different ways.
echo

f=abc g=${f%bc} h="${f%bc}"
echo 'f=abc g=${f%bc} h="${f%bc}"           ==> g is' \"$g\", and h is \"$h\"

f=abc z=bc g=${f%$z} h="${f%$z}"
echo 'f=abc z=bc g=${f%$z} h="${f%$z}"      ==> g is' \"$g\", and h is \"$h\"

f=abc z=bc g=${f%${z}} h="${f%${z}}"
echo 'f=abc z=bc g=${f%${z}} h="${f%${z}}"  ==> g is' \"$g\", and h is \"$h\"

echo

echo These are all the same either within quotes or without. Now, we try it
echo with a nested substitution. First we establish that the inner substitution
echo results in \"bc\", whether quoted or not:
echo

f=abc g=${f#?} h="${f#?}"
echo 'f=abc g=${f#?} h="${f#?}"             ==> g is' \"$g\", and h is \"$h\"
echo

echo And now, we look at what happens that is used as the pattern in the
echo outer substitution. This is the case that fails in dash:
echo

f=abc g=${f%${f#?}} h="${f%${f#?}}"
echo 'f=abc g=${f%${f#?}} h="${f%${f#?}}"   ==> g is' \"$g\", and h is \"$h\"
echo

echo Interestingly, if we try it using a fixed pattern in the inner
echo substitution, it all works:
echo

f=abc g=${f%${f#a}} h="${f%${f#a}}"
echo 'f=abc g=${f%${f#a}} h="${f%${f#a}}"   ==> g is' \"$g\", and h is \"$h\"
echo
