Skip to content

Instantly share code, notes, and snippets.

@mevanlc
Created December 20, 2023 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mevanlc/11d46a4ff9c4e7ed91c1fafcfc448950 to your computer and use it in GitHub Desktop.
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
#!/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