如何使用 Bash 将文本文件的最后一行读入变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42987486/
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-18 15:56:18 来源:igfitidea点击:
How to read the last line of a text file into a variable using Bash?
提问by Tommy Liu
I want to read a text file called history.txt that contains multiple lines and store the content of the last line into a variable called "tag".
我想读取一个名为 history.txt 的文本文件,其中包含多行并将最后一行的内容存储到一个名为“tag”的变量中。
Thanks
谢谢
回答by eftshift0
tag=$( tail -n 1 history.txt )
tag=$( tail -n 1 history.txt )
回答by Zaker typtheitroad
cat FileName | while read oneline
do
echo "a oneline: $line"
done