tags:

views:

28

answers:

1

Hi, from /lib/lsb/init-functions (maybe this file is debian specific, but doesn't really matter for the question):

pidofproc () {
    local pidfile line i pids= status specified pid
    pidfile=
    specified=

Whats the difference between saying

local a

and

local a=

?

+1  A: 

Both types remove any external versions of the variables from the scope. The = assigns a null value to the variable, whereas the bare form leaves the variable unset.

For example:

A=30
B=30

function foo()
{
    local A B=
    echo A - $A
    echo B - $B
    echo A :- ${A:-minusA}
    echo B :- ${B:-minusB}
    echo A :+ ${A:+plusA}
    echo B :+ ${B:+plusB}
    echo A hash ${#A}
    echo B hash ${#B}


    echo A - ${A-minusA}
    echo B - ${B-minusB}
    echo A + ${A+plusA}
    echo B + ${B+plusB}


    ## Modifies variable
    echo A := ${A:=eqA}
    echo B := ${B:=eqB}
    echo A - $A
    echo B - $B
}

foo

Output:

A -
B -
A :- minusA
B :- minusB
A :+
B :+
A hash 0
B hash 0
A - minusA
B -
A +
B + plusB
A := eqA
B := eqB
A - eqA
B - eqB

You can see the section:

    echo A - ${A-minusA}
    echo B - ${B-minusB}
    echo A + ${A+plusA}
    echo B + ${B+plusB}

is different for A and B.

Douglas Leeder