我对"IFS=$'\n‘有问题。
old_IFS=$IFS
file="Good morning
Good morning
Good morning
Good morning
Good morning"
IFS=$'\n'
for line in $file
    do
       echo $line
done
IFS=$old_IFS当我执行脚本时:
Good mor
i
g
Good mor
i
g
Good mor
i
g
Good mor
i
g
Good mor
i
g删除"n“。我想逐行查看这些文件
发布于 2014-05-27 23:28:44
迭代多行数据(无论您的shell是否支持$'...')的正确方法是使用重复调用read的while循环:
while read -r; do
    echo "$REPLY"
done <<EOF
$file
EOF发布于 2014-05-26 23:32:46
正如其他人所指出的,我不认为您正在使用的shell支持$'...'语法。
如果您的系统使用破折号作为sh,您应该能够通过将IFS赋值给一个空的或未赋值的变量来将其赋值给新行。例如,n=""; IFS=$n允许您按新行拆分。这是一个技巧,只有当一个空变量被解释为解释器的新行时才能起作用。
您还可以使用<<<word读取像Bash这样的shell中的here字符串。
file="Good morning
Good morning
Good morning
Good morning
Good morning"
while read line; do
    echo $line
done <<<"$file"否则,您可以从文件中逐行读取。
while read -r line; do 
    echo "$line"
done < filehttps://stackoverflow.com/questions/23873371
复制相似问题