Use Bash to read line by line and keep space

后端 未结 6 892
感情败类
感情败类 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:37

    Maybe IFS is the key point as others said. You need to add only IFS= between while and read.

    cat test.file | 
    while IFS= read data 
     do echo "$data"
     done
    

    and do not forget quotations of $data, else echo will trim the spaces.

    But as Joshua Davies mentioned, you would prefer to use the predefined variable $REPLY.

提交回复
热议问题