[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Understanding read -r
From: |
Bob Proulx |
Subject: |
Re: [Help-bash] Understanding read -r |
Date: |
Thu, 4 Oct 2012 17:16:00 -0600 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Dan Douglas wrote:
> Bob Proulx wrote:
> > Peng Yu wrote:
> > > The following example shows that "\" at the line end means line
> > > continuation. Is this wrong? Or I misunderstand the document?
> >
> > You forgot to quote the EOF part. See the stackoverflow reference.
> > Therefore the shell interpreted the string contents before 'read'
> > processing.
> > > read -r -d '' VAR <<EOF
> > Add quotes there.
> > read -r -d '' VAR <<'EOF'
> > Again, this has to do with the here-document part and not the 'read' part.
>
> This also suppresses the expansions of course. Not sure if you want that.
Of course I was addressing only the specific point that the read had
not been given any backslashes and therefore was not able to read them
verbatim. The issue of quoting is a separate item.
Of course I wasn't sure why there was a need to 'read' into a variable
at that point. But didn't open that question.
> { var=$(</dev/stdin); } <<EOF
> blah
> $foo $bar\
> more text
> EOF
>
> Now both line continuations and expansions are done as you probably
> want them, rather than letting read mangle things without -r.
I personally dislike the use of /dev/stdin and the others because they
are non-portable across kernels and cause me trouble. It is a
specific kernel feature that across the systems I care about is
definitely not portable. YMMV.
> As an aside, mksh has an interesting syntax to assign a heredoc
> result directly to a variable:
>
> # mksh
>
> var=<<EOF
> ...
> EOF
>
> This is optimized to happen in memory with no temporary file.
Looks like a nice feature.
Bob