help-bash
[Top][All Lists]
Advanced

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

Re: Format of here-documents


From: Andreas Kusalananda Kähäri
Subject: Re: Format of here-documents
Date: Tue, 14 Mar 2023 20:49:14 +0100

On Tue, Mar 14, 2023 at 03:01:57PM +0000, goncholden wrote:
> 
> 
> 
> 
> 
> Sent with Proton Mail secure email.
> 
> ------- Original Message -------
> On Wednesday, March 15th, 2023 at 2:28 AM, Andreas Kusalananda Kähäri 
> <andreas.kahari@abc.se> wrote:
> 
> 
> > On Tue, Mar 14, 2023 at 11:58:16AM +0000, goncholden via wrote:
> > 
> > > Have been reading the manual about Here-Documents.
> > > 
> > > The format of here-documents is:
> > > 
> > > [
> > > 
> > > n
> > > 
> > > ]<<[-]
> > > 
> > > word
> > > 
> > > here-document
> > > 
> > > delimiter
> > > 
> > > But in practice I have seen the following
> > > 
> > > cat << _end_of_text
> > > First Line
> > > Second Line
> > > Third Line
> > > _end_of_text
> > > 
> > > Meaning that 'delimiter' is actually '
> > > 
> > > word'. Is this correct ?
> > 
> > 
> > From the bash manual:
> > 
> > If any part of word is quoted, the delimiter is the result of
> > quote removal on word, and the lines in the here-document are
> > not expanded.
> > 
> > Assumign that your issue with the manual is that the displayed syntax
> > for here-documents does not say
> > 
> > [n]<<[-]word
> > here-document
> > word
> > 
> > ... the quoted sentence above should explain why the "word" and
> > "delimiter" may be different (they would be if "word" is quoted in any
> > way).
> 
> I agree, there needs to be more explanation.
> 
> The order should be changed, starting with the simplest state (word and 
> delimiter being the same) first.  Separation between "word unquoted" and 
> "word quoted" is easier to spot whet one is reading rapidly.

Arguably, "simplest" means the contents of the here-document is
redirected as-is, with no expansions.  See also the POSIX standard,
which has paragraph breaks as you describe, but with the sections in the
same general order as the bash manual.  Also note the the lack of saying
what does *not* happen to the word (no expansions, etc.)

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04

> 
> Here is an updated version:
> 
> --------
> 
> No parameter and variable expansion, command substitution, arithmetic 
> expansion, or filename expansion is performed on word. 
> 
> If word is unquoted, the delimiter is the same as word, and all lines of the 
> here-document are subjected to parameter expansion, command substitution, and 
> arithmetic expansion, the character sequence \newline is ignored, and ‘\’ 
> must be used to quote the characters ‘\’, ‘$’, and ‘`’. 
> 
> If any part of word is quoted, the delimiter is the result of quote removal 
> on word, and the lines in the here-document are not expanded. 
> 
> --------

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

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