[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
heredoc in command subst bug?
From: |
Eric Blake |
Subject: |
heredoc in command subst bug? |
Date: |
Thu, 29 May 2008 20:21:56 +0000 (UTC) |
User-agent: |
Loom/3.14 (http://gmane.org/) |
I'm not sure whether this is a bug in POSIX or in bash, but I noticed the
following with bash-3.2.39.
$ bash -c 'foo=$(cat <<EOF
hi
EOF)
echo $foo'
According to
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_0
7_04, heredoc parsing "continues until there is a line containing only the
delimiter and a <newline>", and "EOF)" is not a line containing only the
delimiter and a <newline>. Therefore, per the rules in
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_0
6_03 that the closing ) is not located until a valid shell script has been seen
as the command, this should be interpreted as an unterminated here-doc and an
unterminated command-substitution, and cause a syntax error. But bash treats
the ) as the conclusion of the command-substitution, the EOF followed by end-of-
input as the conclusion of the heredoc, and prints out "hi" without error.
On the other hand, I suppose bash is doing something sensible (pdksh and zsh
had the same behavior; only ash reported a syntax error), since this next
example demonstrates that bash (and ash, zsh, and pdksh) consistently treats
end-of-input as a valid heredoc terminator, even though no newline was present.
$ printf 'cat <<EOF
hi
EOF' > foo
$ chmod +x foo
$ bash ./foo
hi
A similar case occurs with ``:
$ bash -c 'foo=`cat <<EOF
hi
EOF`
echo $foo'
Here again, only ash behaves differently (but by printing "hi EOF" rather than
complaining of a syntax error); but this time, POSIX is explicit that an
unquoted ` in the body of a heredoc causes undefined behavior, and by my
earlier arguments, 'EOF`' is still the body of the heredoc. So this is not a
valid POSIX test case, and bash neither needs to change behavior, nor needs to
keep behavior the same if the $() case is changed.
Finally, bash has a definite bug, with no POSIX ambiguities:
$ bash -c 'cat <<EOF
hi'
Here, bash treats end-of-input as the heredoc delimiter, and prints "hi", even
though it should be complaining of a syntax error since the heredoc (and thus
the script) is unterminated. pdksh behaves correctly and fails with a syntax
error.
--
Eric Blake
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- heredoc in command subst bug?,
Eric Blake <=