在这种情况下 readonly 意味着什么或做什么?我从未见过或听说过它。
a="testString"; b=a; readonly b; b=25;
另外,最后b的最终值会是25吗?
答案1
它使变量只读。从help readonly
:
readonly: readonly [-aAf] [name[=value] ...] or readonly -p
Mark shell variables as unchangeable.
Mark each NAME as read-only; the values of these NAMEs may not be
changed by subsequent assignment. If VALUE is supplied, assign VALUE
before marking as read-only.
Options:
-a refer to indexed array variables
-A refer to associative array variables
-f refer to shell functions
-p display a list of all readonly variables and functions
An argument of `--' disables further option processing.
Exit Status:
Returns success unless an invalid option is given or NAME is invalid.
$b
当以只读方式创建 b 时,将始终返回“1”。举个例子:
$ a=1
$ a=2
$ readonly b=1
$ b=2
bash: b: readonly variable
$ echo "$a"
2
$ echo "$b"
1