Use Bash to read line by line and keep space

后端 未结 6 902
感情败类
感情败类 2020-11-28 05:00

When I use \"cat test.file\", it will show

1
 2
  3
   4

When I use the Bash file,

cat test.file |
while read data
do
    e         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 05:27

    Just to complement DigitalRoss's response.

    For that case that you want to alter the IFS just for this command, you can use curly braces. If you do, the value of IFS will be changed only inside the block. Like this:

    echo '
      word1
      word2' |  { IFS='' ; while read line ; do echo "$line" check ; done ; }
    

    The output will be (keeping spaces):

      word1 check
      word2 check
    

提交回复
热议问题