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:
<<<wordThe 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?
readfails (in the has-a-nonzero-exit-status sense) absent them; multiple standard UNIX tools have undefined behavior when newlines on input are missing. You absolutely won't see this change in future releases.wc -c,md5sum), and so in general command's stdin is not required to be a valid text file. Also Bash doesn't always add a newline (see examples in the post), and so this argument alone doesn't explain much.printf, you're requesting explicit control, which is a very distinct case; in the case ofcat, bash has no control over what a subcommand outputs, and no visibility into it either -- so it couldn't see if there was already a trailing newline to add a missing one even if that were desired!<<<foo$'\n'and it adds the second trailing newline regardless of the fact that the string already has one. Don't you think that disagrees with your explanation?