理解这个bash脚本的结果

理解这个bash脚本的结果

我有这个简单的 bash 脚本

#!/bin/bash -x

for line in `tail /home/user/line`

 do
      echo $line
 done

但不知道为什么它会回显所有单词而不是输出下面的每一行

++ tail /home/user/line
+ for line in '`tail /home/user/line`'
+ echo Linux
Linux
+ for line in '`tail /home/user/line`'
+ echo is
is
+ for line in '`tail /home/user/line`'
+ echo a
a
+ for line in '`tail /home/user/line`'
+ echo crappy
crappy
+ for line in '`tail /home/user/line`'
+ echo OS
OS
+ for line in '`tail /home/user/line`'
+ echo Do
Do
+ for line in '`tail /home/user/line`'
+ echo you
you
+ for line in '`tail /home/user/line`'
+ echo 'agree?'
agree?
+ for line in '`tail /home/user/line`'
+ echo Unix
Unix
+ for line in '`tail /home/user/line`'
+ echo is
is
+ for line in '`tail /home/user/line`'
+ echo much
much
+ for line in '`tail /home/user/line`'
+ echo better
better
+ for line in '`tail /home/user/line`'
+ echo Or
Or
+ for line in '`tail /home/user/line`'
+ echo maybe
maybe
+ for line in '`tail /home/user/line`'
+ echo Solaris
Solaris
+ for line in '`tail /home/user/line`'
+ echo 'OS?'
OS?
+ for line in '`tail /home/user/line`'
+ echo Linux
Linux
+ for line in '`tail /home/user/line`'
+ echo Linux
Linux
+ for line in '`tail /home/user/line`'
+ echo 'Linux!'
Linux!

这是文本文件

Linux is a crappy OS

Do you agree?

Unix is much better
Or maybe Solaris OS?
Linux Linux Linux!

知道为什么它不能逐行回显吗?谢谢

答案1

如果您想打印这些行,您需要使用while read line逐行while read读取,例如:

while read line;do echo $line ;done < /etc/passwd

相关内容