bash 从文件的行中回显

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9559582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:43:54  来源:igfitidea点击:

echo from lines of a file

bashechocat

提问by patz

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

我有一个包含下一个内容的文件“myfile.txt”:

hola mundo
hello word

and i want work with every line

我想处理每一行

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

i hope this give me

我希望这给我

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?

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

ty all

全部

回答by Johannes Weiss

That's better

这样更好

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

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

甚至更好(不启动其他进程,例如子shell和cat):

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

If you prefer oneliners, it's obviously

如果你更喜欢oneliners,那显然是

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