Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: darwin15.6.0
Compiler: clang
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='darwin15.6.0' -DCONF_MACHTYPE='x86_64-apple-darwin15.6.0' -DCONF_VENDOR='apple' -DLOCALEDIR='/usr/local/Cellar/bash/4.4.5/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -DMACOSX -I. -I. -I./include -I./lib -I./lib/intl -I/private/tmp/bash-20161112-61463-r79i9i/bash-4.4/lib/intl -DSSH_SOURCE_BASHRC -Wno-parentheses -Wno-format-security
uname output: Darwin den 15.6.0 Darwin Kernel Version 15.6.0: Thu Sep 1 15:01:16 PDT 2016; root:xnu-3248.60.11~2/RELEASE_X86_64 x86_64
Machine Type: x86_64-apple-darwin15.6.0
Bash Version: 4.4
Patch Level: 5
Release Status: release
Description:
Tested on OSX as well as Ubuntu GNU/Linux 14.4 and Debian Jessie 8
Tested with Bash 4.3 and 4.4
When trying to output a string from a variable expansion that is inside a here doc, the quotes don't behave the same as when not in a here doc.
Repeat-By:
Here is a small test script that demonstrates the problem:
#!/bin/bash
export VARIABLE="test test"
echo "Trying to quote inside a variable expansion inside a here document"
echo
echo The desired output is:
echo "var = \"test test\""
echo
echo "First off, taking here docs out of the equation, unescaped quotes don't work:"
echo ${VARIABLE:+var = "$VARIABLE"}
echo
echo But escaping the quotes does work:
echo ${VARIABLE:+var = \"$VARIABLE\"}
echo
echo But things get weird when the variable expansion is inside a here doc:
cat << EOF
Plain quotes don't work:
echo ${VARIABLE:+var = "$VARIABLE"}
Escaped quotes don't work:
echo ${VARIABLE:+var = \"$VARIABLE\"}
Double quotes within single quotes don't work:
echo ${VARIABLE:+'var = "$VARIABLE"'}
Even trying to single quote just the double quote is a no-go:
echo ${VARIABLE:+var = '"'$VARIABLE'"'}
I tried a couple of variants of \x22 and $'\x22', but I wasn't sure about the syntax.
Here is the only thing I have found so far that does work:
${VARIABLE:+var = $(echo '"')$VARIABLE$(echo '"')}
EOF
-Daniel