是什么`声明-r`差异和`readonly`在bash? [英] what is difference in `declare -r` and `readonly` in bash?
本文介绍了是什么`声明-r`差异和`readonly`在bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在bash中,是什么的差异声明-r
和只读
?
In bash, what is the difference in declare -r
and readonly
?
$ declare -r a=a1
$ readonly b=b1
我不知道选哪个。
I'm not sure which to choose.
推荐答案
答:范围
乍一看,没什么区别。
使用检查申报-p
$ declare -r a=a1
$ readonly b=b1
$ declare -p a b
declare -r a="a1"
declare -r b="b1"
不过,范围不同。
But, scope is different
$ function f() {
declare -r c=c1
readonly d=d1
echo "f:" c $c d $d
}
$ echo c $c d $d
c d
# verify $c $d not set
$ f
f: c c1 d d1
# prints as expected
$ echo c $c d $d
c d d1
# $d still has a value!
所以一个区别是只读
会使变量的作用域的全球的。 声明
使得变量的作用域的本地的(预计)。
So one difference isreadonly
will make the variable scope global. declare
makes variable scope local (which is expected).
这篇关于是什么`声明-r`差异和`readonly`在bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文