bash 如何在shell脚本中声明一个常量变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7023025/
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
How do I declare a constant variable in shell script?
提问by Oh Chin Boon
I can't find this information from the internet, is it not possible to declare a final constant variable whose value will not change after first initialization?
我在互联网上找不到这个信息,是不是可以声明一个在第一次初始化后值不会改变的最终常量变量?
回答by diagonalbatman
I believe you can do something like:
我相信你可以做这样的事情:
readonly DATA=/usr/home/data/file.dat
You can also do:
你也可以这样做:
declare -r var=123
回答by Anders Lindahl
回答by al.
In bash
you can mark a variable read only by declaring it via the builtin readonly
like so:
在bash
你可以标记一个变量只读经由内建宣布它readonly
就像这样:
readonly CONSTVAR=value