shell分割IFS

一个文件file,内容: 

hello this is a word
i have a apple
do you want one

如果这是读取文件,输出每行

for cont in `cat file`
do
   echo $cont
done

输出内容会是这样的

hello
this
is
a
word
i
have
a
apple
do
you
want
one

要想按行输出,要在脚本前面加上这句,重新定义IFS变量

IFS=$'\n'
介绍

IFS=‘\n’  //将字符n作为IFS的换行符。

IFS=$"\n" //这里\n确实通过$转化为了换行符,但仅当被解释时(或被执行时)才被转化为换行符。

IFS=$'\n' //这才是真正的换行符。

 

posted on 2018-01-03 22:09  思此狂  阅读(243)  评论(0编辑  收藏  举报

导航