为什么从stdin读取bash中的echo字符串不显示空格字符

wuk*_*ong 3 bash shell

这是我的代码

michal@argon:~$ cat test.txt 
1
  2
    3
      4
        5
michal@argon:~$ cat test.txt | while read line;do echo $line;done > new.txt
michal@argon:~$ cat new.txt
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

我不知道为什么命令echo $line过滤了空格字符,我希望test.txt和new.txt完全相同.

请帮忙,谢谢.

n. *_* m. 6

您的代码有几个问题.

  1. $parameter在...之外" ".别.
  2. read用于$IFS将输入行拆分为单词,您必须禁用它.
  3. UUOC

总结一下:

 while IFS= read line ; do echo "$line" ; done < test.txt > new.txt
Run Code Online (Sandbox Code Playgroud)