Reputation: 1165
I found following code in /etc/init.d/functions on CentOS.
status() {
local base pid lock_file= pid_file=
...
4 variables are declared. Two of them are not initialized, base and pid. But rest of them are initialized with empty value, lock_file and pid_file.
I tested following code and found no differences.
local a b=
echo "a is $a, length is ${#a}"
echo "b is $b, length is ${#b}"
Is there any differences between them?
Upvotes: 3
Views: 512
Reputation: 86413
Yes, there is a difference. Consider the following function:
x() {
local a b=
echo ${a-X}
echo ${b-X}
}
Calling this function in bash-4.x
results in this output:
$ x
X
$
The ${parameter−word}
parameter expansion expands to the expansion of word
(in this case X
) if the parameter is unset, or to the parameter value if it is set.
From the example output, it is obvious that local a
leaves the variable a
unset, while local b=
explicitly sets it to the empty (null) string.
EDIT:
On the other hand, on bash-3.x
you get this:
$ x
$
A call to set
within the function verifies that local a
in bash-3.x
initializes that variable to the empty string. This, however, seems to have been a bug. From the bash
changelog:
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.
Upvotes: 3