我的输入文件内容是
欢迎
欢迎1
欢迎2
我的剧本是

for groupline in `cat file`
do
        echo $groupline;
done

我得到了以下输出。
欢迎
欢迎1
欢迎2
为什么不打印空行。我想知道原因。

最佳答案

您需要将IFS设置为newline\n

IFS=$"\n"
for groupline in $(cat file)
do
        echo "$groupline";
done

或者加双引号。有关说明,请参见here
for groupline in "$(cat file)"
do
        echo "$groupline";
done

在不干涉IFS的情况下,“正确的”方法是使用while read循环
while read -r line
do
 echo "$line"
done <"file"

关于bash - 猫与新行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2460377/

10-10 17:43