Unexpected history expanded in heredoc in $() or <()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Unexpected history expanded in heredoc in $() or <()

ladyrick
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:&nbsp; -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H&nbsp; &nbsp;-I.&nbsp; -I../. -I.././include -I.././lib&nbsp; -Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -fdebug-prefix-map=/build/bash-7fckc0/bash-4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -no-pie -Wno-parentheses -Wno-format-security
uname output: Linux n227-006-177 4.14.81.bm.14-amd64 #1 SMP Debian 4.14.81.bm.14 Sun Aug 25 08:22:56 UTC 2019 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu


Bash Version: 4.4
Patch Level: 12
Release Status: release


Description:
        A heredoc starts with "cat <<'EOF'" is expected to not expand anything just like in a single quote string. But when this heredoc is in a $() or <(), history is expanded.


Repeat-By:
        This works:
```
cat <<'EOF'
!!
EOF
```
        This doesn't work:
```
cat <(cat <<'EOF'
!!
EOF
)
```
        This doesn't work neither:
```
echo "$(cat <<'EOF'
!!
EOF
)"
```
        Only when I exec `set +H` to disable history expand, these examples all work.
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected history expanded in heredoc in $() or <()

Dale R. Worley
"ladyrick" <[hidden email]> writes:

> Description:
> A heredoc starts with "cat <<'EOF'" is expected to not expand
> anything just like in a single quote string. But when this
> heredoc is in a $() or <(), history is expanded.
>
>
> Repeat-By:
> This works:
> ```
> cat <<'EOF'
> !!
> EOF
> ```
> This doesn't work:
> ```
> cat <(cat <<'EOF'
> !!
> EOF
> )
> ```
> This doesn't work neither:
> ```
> echo "$(cat <<'EOF'
> !!
> EOF
> )"
> ```

It's a messy case; what's really happening the the last two examples is
that the entire thing is read and processed as one "line", because bash
reads all of the command or process substitution as a unit.  Then it
performs history expansion, and then it interprets the line.  So the
history expansion is done before bash even recognizes that there is a
here-doc.

In the first example, bash reads "cat <<'EOF'" and starts processing
it.  At that point, it recognizes that it must read a here-doc and does
so.

Dale

Reply | Threaded
Open this post in threaded view
|

Re: Unexpected history expanded in heredoc in $() or <()

Chet Ramey
In reply to this post by ladyrick
On 5/29/20 11:59 AM, ladyrick wrote:

> Bash Version: 4.4
> Patch Level: 12
> Release Status: release
>
>
> Description:
> A heredoc starts with "cat <<'EOF'" is expected to not expand anything just like in a single quote string. But when this heredoc is in a $() or <(), history is expanded.

It's an interesting question. While quoted here-document delimiters do
suppress expansion, unquoted delimiters result in expansion similar to
double quotes, and double quotes don't suppress history substitution.
Yet, when not in a command substitution, history substitution is never
performed on the lines in a here-document.

But consistency is important, and I agree that the expansion should not
take place while parsing command or process substitutions, whether or not
the delimiter is quoted.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [hidden email]    http://tiswww.cwru.edu/~chet/