2

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?

10
  • Newlines are required for a file or stream to be valid text-file content in UNIX. read fails (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. Oct 20, 2015 at 20:06
  • 1
    @CharlesDuffy Haven't thought about that, thanks. However, not every tool works on text files (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.
    – eush77
    Oct 20, 2015 at 20:21
  • Chepner's historical background for herestrings as syntactic sugar for one-line heredocs is spot on. Oct 20, 2015 at 20:23
  • ...that said, I don't see any of the examples in the question countering my explanation. In the case of printf, you're requesting explicit control, which is a very distinct case; in the case of cat, 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! Oct 20, 2015 at 20:25
  • 1
    @CharlesDuffy However, Bash is the one expanding <<<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?
    – eush77
    Oct 20, 2015 at 20:32

2 Answers 2

6

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

cat <<< foo

and

cat <<EOF
foo
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 $?; }.)

1
  • Thank you! This seems like the most plausible explanation.
    – eush77
    Oct 22, 2015 at 9:44
2

So my question is: is this behavior specified anywhere in Bash docs? Can I depend on it in scripts?

The specification has now been added to the Bash Reference Manual:

The result is supplied as a single string, with a newline appended, to the command on its standard input (or file descriptor n if n is specified).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.