I came across the following syntax in a here document not mentioned in bash
’s man page
cat <<\EOF
hello world
EOF
The man page only mentions quotes around the delimiter and a -
in front of it. So what does it mean?
I came across the following syntax in a here document not mentioned in bash
’s man page
cat <<\EOF
hello world
EOF
The man page only mentions quotes around the delimiter and a -
in front of it. So what does it mean?
In fact the man page is thorough about this since it reads
If any part of word is quoted
where »quoting« can be any operator of '
, "
or \
.
\EOF
quotes E
and serves the same purpose as quoting WORD
entirely thus preventing parameter expansion in the here document.
a="something"
cat <<\EOF
$a
EOF
and
a="something"
cat <<"EOF"
$a
EOF
both will result in
$a
rather than
something
as would be the case with
cat <<EOF
$a
EOF
In fact, since »any part of WORD « can be quoted you may even use <<E\OF
, <<E"O"F
or <<EOF""
'...'
, "..."
(also $'...'
/ $"..."
in some shells). So any of cat << \EOF
(only E
is quoted), cat << E\OF
, same as cat << E'O'F
, even cat << EOF''
will do. Personally, I tend to use cat << 'EOF'
where the entirety of EOF
is quoted, same as cat << \E\O\F
– Stéphane Chazelas
Sep 30 '21 at 12:57
"EOF"
also suppress variable expansion? That's slightly unexpected.
– u1686_grawity
Oct 01 '21 at 05:07
$'...'
should count as a fourth mechanism, but for whatever reason they're not included in that count.) – ilkkachu Sep 30 '21 at 13:19