从文件行中回显

问题描述:

我有一个文件"myfile.txt",其中包含下一个内容:

i have a file "myfile.txt" that have the next content:

hola mundo
hello word

我想处理每一行

for i in `cat myfile.txt`; do echo $i; done

我希望这能给我

hola mundo
hello word

先打出一行,再打另一行,但得到

firts one line, then the other, but get

hola
mundo
hello
word

因为我可以要求结果直到换行符而不是每个空格?

as I can demanding results until newline instead of each space?

全部

那更好

cat myfile.txt | while read line; do
    echo "$line"
done

甚至更好(不会启动其他进程,例如subshel​​l和cat):

or even better (doesn't launch other processes such as a subshell and cat):

while read line; do
    echo "$line"
done < myfile.txt

如果您喜欢单线客,显然是

If you prefer oneliners, it's obviously

while read line; do echo "$line"; done < myfile.txt