bash - Using local variables from "child" functions -


consider following code:

function child() {     echo $var }  function parent() {     local var=5     child } 

i've tested on machine , seems work wasn't able find definitive describing such usage of local variables. namely, when declare local variable in 1 function , function call other function, can use variable in latter (and nest deeper)? legal in bash , standard versions?

bash uses dynamic scoping. value of var in child not determined child defined, called. if there no local definition in body of child, next place shell looks in body of function child called, , forth. local modifier creates variable in function local call, not affect value of variable enclosing scopes. is, though, visible enclosed scope.

a () { echo "$var"; } b () { local var="local value"; a; }  var="global value"  # outputs "global value" b  # outputs "local value" 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -