Bourne | Ash |  #!  | find | ARG_MAX | Shells | whatshell | portability | permissions | UUOC | ancient | - | ../Various | HOME
"$@" | echo/printf | set -e | test | tty defs | tty chars | $() vs ) | IFS | using siginfo | nanosleep | line charset | locale


Parameter substitution inside here-document inside command substitution inside double quotes


In most traditional Bourne shells the following reveals a bug which is related to the shell internal quoting mechanism:


  set 123b
  echo "x`cat<<EOF
  $1
  EOF
  `x"
or more readable:
  function()
  {
    set 123b
    cat<<EOF
    $1
    EOF
  }
  echo "x`function`x"


Instead of the expected output

"x123bx"

you get one of

"x\1\2\3\bx"
"x\1\2\x"

depending on whether '\b' is interpreted as Backspace by the echo built-in. (And the example uses echo by intention to remind of this pitfall.)

You cannot try this in shells before SVR1, because they were not fixed about the bug with "`cat<<...`" yet.

In SVR2 shells which are not 8bit-clean, you get

"x±²³âx" (inner characters have the 8th bit set),

because the 8th bit was internally used for the quoting mechanism.


This issue was fixed at least on AIX 3, HP-UX 10 and OSF1/V4.


A possible workaround is taking the command substitution out of the double quotes, that is,
  [...]
  echo "x"`function`"x"

2003-03-01