Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Expanded behavior for $ and \$ in heredoc.

6 views
Skip to first unread message

Hongyi Zhao

unread,
Apr 1, 2015, 10:20:46 PM4/1/15
to
Hi all,

See the following snippets:

cat << EOF
\$
$
EOF

Bu running it, I will obtain the following result:

$
$

So, if I want to obtain the literal character `$' in heredoc with its
expanded behavior enables, which form should I use?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Kaz Kylheku

unread,
Apr 1, 2015, 10:47:57 PM4/1/15
to
On 2015-04-02, Hongyi Zhao <hongy...@gmail.com> wrote:
> Hi all,
>
> See the following snippets:
>
> cat << EOF
> \$
> $
> EOF
>
> Bu running it, I will obtain the following result:
>
> $
> $
>
> So, if I want to obtain the literal character `$' in heredoc with its
> expanded behavior enables, which form should I use?

Your requirement is contradictory. If the character $ is "literal",
that precisely means that its special behavior is disabled.

# cat << EOF
$TERM
\$TERM
EOF
xterm
$TERM

If $ is followed by whitespace, or the end of the line, then even if it is not
escaped, it just denotes itself:

# echo $ $
$ $

Janis Papanagnou

unread,
Apr 1, 2015, 11:16:40 PM4/1/15
to
On 02.04.2015 04:20, Hongyi Zhao wrote:
> Hi all,
>
> See the following snippets:
>
> cat << EOF
> \$
> $
> EOF
>
> Bu running it, I will obtain the following result:
>
> $
> $
>
> So, if I want to obtain the literal character `$' in heredoc with its
> expanded behavior enables, which form should I use?

It matters if the '$' is not alone but in some expansion context. Observe
the effect of escaping the '$' and also the escaping of the here-doc tag:

x=X
cat <<EOT
\$x=$x
EOT
cat <<\EOT
\$x=$x
EOT

Results in:
$x=X
\$x=$x

(The explanation can of course also be found in the various manuals.)

Janis

>
> Regards
>

Hongyi Zhao

unread,
Apr 1, 2015, 11:44:49 PM4/1/15
to
On Thu, 02 Apr 2015 05:16:37 +0200, Janis Papanagnou wrote:

> It matters if the '$' is not alone but in some expansion context.
> Observe the effect of escaping the '$' and also the escaping of the
> here-doc tag:

Thanks for this hints.

>
> x=X cat <<EOT \$x=$x EOT cat <<\EOT \$x=$x EOT
>
> Results in:
> $x=X \$x=$x

Again, thanks for this subtle example.

>
> (The explanation can of course also be found in the various manuals.)
>
> Janis





0 new messages