No, here-strings come from zsh
in 2.0 in 1991 (and/or the Unix port of rc
, their respective author exchanging ideas around that time, it's not clear which of the two had the idea or included it in his shell first).
It was added to bash in 2.05b (2002), ksh93 in m+ (2002), mksh in R33 (2008), yash in 2.7 (2009).
ksh88 is not getting any new features.
Here documents (<<
), themselves come from the Bourne-shell in the late 70s.
read A B C
reads one logical line into the A
, B
and C
variables in a very special way (a bit less special with the default value of $IFS
that contains only IFS-white-space characters¹), with backslash acting as an escaping and line-continuation character and what's going into C
being quite complex as well. Doing the same thing without read
would quite difficult.
Here anyway, <<<
is just the same as <<
, with only syntactic differences.
read A B C << EOF
$var
EOF
is exactly the same as
read A B C <<< "$var" # note that some versions of bash need the quotes
in all shells. For both, the shell creates a deleted temporary file with the content and an additional newline (though some shells use pipes instead). Then read
reads one logical line from it (possibly on several physical lines continued with backslash) and fills in the variables using its complex rules.
It only assigns the first 3 words (SPC/TAB delimited) of $var
to $A
, $B
and $C
if
$IFS
still contains its default value
- the variable contains no backslash and no newlines
- the variable contains only 3 words.
For the first three words, you could do:
function split_into {
typeset words IFS v i=0
set -o noglob
set -A words -- $1; shift
for v do
eval "$v=\${words[i]}"
((i += 1))
done
}
split_into "$var" A B C
¹ IFS whitespace characters, per POSIX being the characters classified as [:space:]
in the locale and that happen to be in $IFS
though in ksh88 (on which the POSIX specification is based) and in most shells, that's still limited to SPC, TAB and NL. The only POSIX compliant shell in that regard I found was yash
. ksh93
and bash
(since 5.0) also include other whitespace (such as CR, FF, VT...), but limited to the single-byte ones (beware on some systems like Solaris, that includes the non-breaking-space which is single byte in some locales)