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

筅森魡賤 提交于 2019-12-06 15:52:31

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

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!