Having read the following description of this feature of Bash (excerpt from the man page):
Here Strings
A variant of here documents, the format is:
<<<word
The word is expanded and supplied to the command on its standard input.
I expected that the interpretation of here strings is that Bash simply passes the contents of a variable directly on a command's standard input, unmodified. Following this logic, the lines [1]
and [2]
below would be effectively equivalent.
[1]~$ printf foo | cat - <(echo end)
fooend
[2]~$ cat - <(echo end) <<<foo
foo
end
However, Bash added a newline when “expanding” a string, something I didn't anticipate. This happens even when a variable ends with newline itself:
[3]~$ printf "foo\n" | cat - <(echo end)
foo
end
[4]~$ cat - <(echo end) <<<foo$'\n'
foo
end
Tested in 4.2.25 and 4.3.30.
So my question is: is this behavior specified anywhere in Bash docs? Can I depend on it in scripts?