Page MenuHomeFreeBSD

sh: Fix heredoc at certain places in case and for

Authored by jilles on Oct 24 2021, 9:57 PM.
Referenced Files
Unknown Object (File)
Wed, Nov 29, 6:27 AM
Unknown Object (File)
Wed, Nov 29, 1:55 AM
Unknown Object (File)
Wed, Nov 29, 12:05 AM
Unknown Object (File)
Fri, Nov 24, 12:03 AM
Unknown Object (File)
Tue, Nov 21, 10:39 PM
Unknown Object (File)
Oct 31 2023, 12:37 PM
Unknown Object (File)
Oct 30 2023, 11:36 AM
Unknown Object (File)
Oct 24 2023, 11:33 PM



After an unescaped newline, there may be a here-document. Some places in
case and for did not check for one.

Test Plan

Testsuite, including three new tests

Diff Detail

rG FreeBSD src repository
Lint Not Applicable
Tests Not Applicable

Event Timeline

The here-document shall be treated as a single word that begins after the next <newline>

Huh interesting. I did not know this. dash (git) fails on heredoc14.0 but the rest of the shells, and other scripts, pass outside of sh.
I was surprised about this but there is a clear example at that doc showing ; is allowed.

An example of a here-document follows:

cat <<eof1; cat <<eof2

dash also has CHKKWD | CHKALIAS here but I don't see how a keyword or alias is valid here.


And dash checks for CHKKWD here too but seems wrong.

This revision is now accepted and ready to land.Oct 26 2021, 5:06 PM

Like most shells, dash recognizes in as a keyword, resulting in a syntax error if it occurs unquoted as the first word of a command. zsh and FreeBSD sh only recognize in in the appropriate location in a case or for command, allowing it unquoted as a utility or function name.

Recognizing aliases here is an extension shared by a few other shells. In older shells, this happened because keyword and alias recognition were tied together. For example,

sh -c 'alias DO=do; eval "$1"' arg0 'for i in a b c; DO echo ".$i"; done'

works with a variety of shells, and

sh -c 'alias IN=in; eval "$1"' arg0 'for i IN a b c; do echo ".$i"; done'

works a bit less commonly. Per POSIX, this is allowed because the syntax is otherwise invalid. Expanding an alias in in a for command would not be allowed, however.