Thursday, January 8, 2015

Bash Localized Variable Occlusion

function update() { 
local -i VAR=45
VAR+=-1
VAR+=$1
echo $VAR
} 
 
$ VAR=3
$ update VAR 
 
And the output is??  
.. 88  
Why? Because $1 is VAR and the local VAR takes precedence control over 
VAR definitions, after dereferencing $1 the local VAR (now at 44) is added to itself. 

No comments:

Post a Comment