开发者

Difference between local a and local a=

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

pidofp开发者_StackOverflowroc () {
    local pidfile line i pids= status specified pid
    pidfile=
    specified=

Whats the difference between saying

local a

and

local 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜