问题描述
看了巴什(摘自该名男子页)此功能的以下说明:
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?
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 $?; }
.)
这篇关于为什么猛砸一直在这里扩大字符串时,添加一个新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!