为什么读取来自管道和heredoc的相同输入的行为不同:
printf "" | while read line; do echo "line=$line"; done # outputs nothing
while read line; do echo "line=$line"; done <<< "" # outputs 'line='
在第二种情况下如何禁用输出?
这里的文档\n
末尾有一个隐含的换行符 ( );printf ""
什么都不输出。我不知道摆脱隐式换行符的方法。
如果您可以丢弃所有空行...
while read line; do if test -n "$line"; then echo "line=$line"; fi; done <<< ""
如何使用$'\c'
:
man bash | less -p '\\c * suppress trailing newline'
str=""
while read line; do echo "line=$line"; done <<<$'\c'"${str}"
str="abc"
while read line; do echo "line=$line"; done <<<$'\c'"${str}"