[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unclosed quotes on heredoc mode

From: Chet Ramey
Subject: Re: Unclosed quotes on heredoc mode
Date: Wed, 17 Nov 2021 15:17:29 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.3.0

On 11/17/21 1:45 PM, João Almeida Santos wrote:
> No, it’s on the email...Anyway, here’s the text!
> bash-5.1$ echo $PATH
> /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/:/usr/local/bin/:/usr/local/bin/
> bash-5.1$ cat << $PATH
>> /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/:/usr/local/bin/:/usr/local/bin/
>> it should have terminated with the upper delimiter! but, bash does not seem 
>> to expand PATH.
>> $PATH
> /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/:/usr/local/bin/:/usr/local/bin/
> it should have terminated with the upper delimiter! but, bash does not seem 
> to expand PATH.

The here document delimiter does not undergo any expansions except quote
removal, so the delimiter is the literal string `$PATH'. The lines of the
here-document undergo a different set of expansions, which happen after the
check for the delimiter is performed, which means that you need to have a
line that consists solely of `$PATH' to terminate the here-document (as you
discovered). I cannot see how you're going to be able to do anything useful
with this construct; it just seems too clever by (more than) half.

This is all in the bash documentation.

> ok, but now addressing the actual question. If I use unclosed quotes on 
> heredoc, I can't use 
> the given delimiter to end the heredoc, I end up having to use an EOF. 
> Example:
> bash-5.1$
> bash-5.1$ cat « ola"
>> I,
>> ola""
>> ola"
>> ola

The delimiter is not what you think it is. The delimiter for a here-
document is a shell word (which can include quoted substrings), and after
it undergoes the appropriate quote removal, your delimiter is
"ola\nI,\nola\nola" (using C string notation).

Now, you're never going to be able to match this; it contains a newline.
When the shell constructs the here-document body, it reads individual lines
from the input source and, after removing the trailing newline, tries to
match them against the delimiter (and backslash doesn't work to quote the
newline). This will obviously never match a delimiter containing a newline.

Some shells (e.g., yash) choose to make this a syntax error. Bash does not.

> In the above example, I don't unterstand how to provide the wanted delimiter!

You simply cannot, not the way you specify it. If you really want to have
the double quote as part of the here-document delimiter, write it as

cat << ola\"

I can't imagine this being useful, either.

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]