bash 从文件中读取字符串并转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17658079/
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
Read string from file and convert into an int
提问by Ghost
I have a txt file with ONLY a number inside; it can be any number, how can i read it from bash and convert it into an int?
我有一个 txt 文件,里面只有一个数字;它可以是任何数字,我如何从 bash 读取它并将其转换为 int?
As i said, the only thing in the file is the number, no other thing, but when reading it, bash gets it as a string.
正如我所说,文件中唯一的东西是数字,没有其他东西,但是在读取它时,bash 将其作为字符串获取。
Thanks in advance!
提前致谢!
回答by Barmar
Use declareor typeset:
使用declare或typeset:
typeset -i variable=$(cat filename)

