shell中for循环,读取一整行

摘自:https://blog.csdn.net/peterxiaoq/article/details/77247547

shell中for循环的默认分隔符是:空格、tab、

需求是只以 作为分隔符

shell for循环以 作为分割符,

方式一:

文件aa.sh

#!/bin/bash  
  
  
IFS=$'

'  
  
for i in `cat 1.txt`;  
do  
    echo "begin"  
    echo $i  
    echo "end"  
done  

运行方式也要注意:./aa.sh 或  bash aa.sh

不要使用sh aa.sh, 为啥?因为无效!


方式二:

#!/bin/bash  
  
while read i;    
do  
    echo "begin"  
  echo $i  
   a=`echo $i | cut -f 1 -d " "`  
   echo $a  
   b=`echo $i | cut -f 2 -d " "`  
   echo $b  
   echo "end"  
done<1.txt  
原文地址:https://www.cnblogs.com/LiuYanYGZ/p/12577350.html