我在CentOS的/etc/init.d/functions中找到以下代码.
status() {
local base pid lock_file= pid_file=
...
声明了4个变量.
其中两个未初始化,即base和pid.
但是其余的都使用空值,lock_file和pid_file初始化.
我测试了以下代码,没有发现任何区别.
local a b=
echo "a is $a, length is ${#a}"
echo "b is $b, length is ${#b}"
它们之间有什么区别吗?
解决方法:
是,有一点不同.考虑以下功能:
x() {
local a b=
echo ${a-X}
echo ${b-X}
}
在bash-4.x中调用此函数将得到以下输出:
$x
X
$
如果未设置参数,则${parameter−word} parameter expansion扩展为word的扩展名(在本例中为X),如果已设置,则扩展为参数值.
从示例输出中可以明显看出,局部a使变量a保持未设置状态,而局部b =将其显式设置为空(空)字符串.
编辑:
另一方面,在bash-3.x上,您得到以下信息:
$x
$
在函数内对set的调用将验证bash-3.x中的local a将该变量初始化为空字符串.但是,这似乎是一个错误.从bash更新日志中:
This document details the changes between this version, bash-4.0-beta,
and the previous version, bash-4.0-alpha.
…
e. Fixed a bug that caused local variables to be created with the empty
string for a value rather than no value.
标签:shell,centos,linux
来源: https://codeday.me/bug/20191127/2076976.html