From https://unix.stackexchange.com/a/381782/674
For instance:
integer() { typeset -gi "$1"; }To make a variable an integer works in
mksh/yash/zsh. It works inbashonly on variables that have not been declared local by the caller:$ bash -c 'f() { declare a; integer a; a=1+1; echo "$a"; }; integer() { typeset -gi "$1"; }; f' 1+1 $ bash -c 'f() { integer a; a=1+1; echo "$a"; }; integer() { typeset -gi "$1"; }; f' 2Note that
export varis neithertypeset -x varnortypeset -gx var. It adds theexportattribute without declaring a new variable if the variable already existed. Same forreadonlyvstypeset -r.
For bash,
inside
fof the first example, what doesinteger ado, to declare a differentafrom theadeclared insidefor to make theadeclared insidefhave the global scope? Why does it output1+1?inside
fof the second example, doesinteger adeclareawith the global scope? Why does it output2?
For zsh, the same questions, except that why the first example outputs 2 instead of 1+1 as for bash?
Am I correct that
- both bash and zsh use dynamic scoping, at least in the examples?
- the option
-goftypesetin both bash and zsh means to declare a nonexisting variable with the global scope, or to change an existing variable to have the global scope?
Thanks.