Created
December 20, 2023 09:48
-
-
Save mevanlc/11d46a4ff9c4e7ed91c1fafcfc448950 to your computer and use it in GitHub Desktop.
Testing the usage of global and local variables inside and outside functions in a number of combinations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
decl_outside="assigned outside a function" | |
echo "printing from outside: \$decl_outside: value before assignment INSIDE a function: [$decl_outside]" | |
INSIDE1() { | |
echo "printing from INSIDE1(): \$decl_outside: value before assignment by INSIDE1(): [$decl_outside]" | |
decl_outside="assigned by INSIDE1()" | |
echo "printing from INSIDE1(): \$decl_outside: value after assignment by INSIDE1(): [$decl_outside]" | |
decl_inside="assigned by INSIDE1()" | |
echo "printing from INSIDE1(): \$decl_inside: value after assignment by INSIDE1(): [$decl_inside]" | |
local local_decl_inside="LOCAL-assigned by INSIDE1()" | |
echo "printing from INSIDE1(): \$local_decl_inside: value after LOCAL-assignment by INSIDE1(): [$local_decl_inside]" | |
} | |
INSIDE1 | |
echo "printing from outside: \$decl_outside: value after assignment INSIDE a function: [$decl_outside]" | |
echo "printing from outside: \$decl_inside: value after assignment INSIDE a function: [$decl_inside]" | |
echo "printing from outside: \$local_decl_inside: value after LOCAL-assignment INSIDE a function: [$local_decl_inside]" | |
INSIDE2() { | |
echo "printing from INSIDE2(): \$decl_outside: value before LOCAL-assignment by INSIDE2(): [$decl_outside]" | |
local decl_outside="LOCAL-assigned by INSIDE2()" | |
echo "printing from INSIDE2(): \$decl_outside: value after LOCAL-assignment by INSIDE2(): [$decl_outside]" | |
} | |
INSIDE2 | |
echo "printing from outside: \$decl_outside: value outside a function after LOCAL-assignment INSIDE a function: [$decl_outside]" | |
INSIDE3() { | |
local decl_outside="LOCAL-assigned by INSIDE3()" | |
echo "printing from INSIDE3(): \$decl_outside: value after LOCAL-assignment by INSIDE3(): [$decl_outside]" | |
unset decl_outside | |
echo "printing from INSIDE3(): \$decl_outside: value after 1x UNSET INSIDE by INSIDE3(): [$decl_outside]" | |
} | |
INSIDE3 | |
echo "printing from outside: \$decl_outside: value outside a function after 1x UNSET INSIDE a function: [$decl_outside]" | |
INSIDE4() { | |
local decl_outside="LOCAL-assigned by INSIDE4()" | |
echo "printing from INSIDE4(): \$decl_outside: value after LOCAL-assignment by INSIDE4(): [$decl_outside]" | |
unset decl_outside | |
echo "printing from INSIDE4(): \$decl_outside: value after 1x UNSET INSIDE by INSIDE4(): [$decl_outside]" | |
unset decl_outside | |
echo "printing from INSIDE4(): \$decl_outside: value after 2x UNSET INSIDE by INSIDE4(): [$decl_outside]" | |
} | |
INSIDE4 | |
echo "printing from outside: \$decl_outside: value outside a function after 2x UNSET INSIDE a function: [$decl_outside]" | |
INSIDE5() { | |
unset decl_outside | |
echo "printing from INSIDE5(): \$decl_outside: value after 1x NONLOCAL UNSET INSIDE a function: [$decl_outside]" | |
} | |
INSIDE5 | |
echo "printing from outside: \$decl_outside: value outside a function before 1x NONLOCAL UNSET INSIDE a function: [$decl_outside]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment