{
IFS= read -r a
IFS= read -r b
IFS= read -r c
} <<'END_INPUT'
line 1
line 2
line 3
END_INPUT
printf '<%s> <%s> <%s>\n' "$a" "$b" "$c"
将此推广到任意数量的行:
set --
while IFS= read -r line; do
set -- "$@" "<$line>" # cheating here by adding on the "<...>"
done <<'END_INPUT'
line 1
line 2
line 3
line 4
END_INPUT
printf '%s\n' "$*"
您正在阅读三行,这意味着调用
read
三次:将此推广到任意数量的行:
"$*"
将扩展为一个单引号字符串,该字符串由所有由第一个字符分隔的位置参数组成$IFS
(默认为空格)。