如何在Shell脚本中声明常量?


81

我无法从互联网上找到此信息,是否无法声明最终常量变量,其值在首次初始化后不会更改?

Answers:


116

我相信您可以执行以下操作:

readonly DATA=/usr/home/data/file.dat

您也可以这样做:

declare -r var=123


15

在In中,bash您可以通过内置变量将变量声明为只读,readonly如下所示:

readonly CONSTVAR=value
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.