eush77 eush77 - 1 year ago 100
Bash Question

Why does Bash always add a newline when expanding here strings?

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:


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
below would be effectively equivalent.

[1]~$ printf foo | cat - <(echo end)
[2]~$ cat - <(echo end) <<<foo

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)
[4]~$ cat - <(echo end) <<<foo$'\n'


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?

Answer Source

Not definitive, but I believe a here string is intended to be equivalent to a single-line here document, so that

cat <<< foo


cat <<EOF

are equivalent. Since a here document always ends with a newline, so should the here string.

Consider this simple use case for a here string:

IFS=: read foo bar <<< "a:b"
# foo=a
# bar=b

If a newline weren't provided by the here string, the exit status of read would be 1. (See with printf "foo" | { read; echo $?; } vs printf "foo\n" | { read; echo $?; }.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download