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

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

How do I declare a constant variable in shell script?

bashshell

提问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

只读FOO=bar

​​​​​

​​​​​

回答by al.

In bashyou can mark a variable read only by declaring it via the builtin readonlylike so:

bash你可以标记一个变量只读经由内建宣布它readonly就像这样:

readonly CONSTVAR=value