mirror of https://github.com/tLDP/LDP
30 lines
996 B
Bash
30 lines
996 B
Bash
#!/bin/bash
|
|
# ex62.sh: Global and local variables inside a function.
|
|
|
|
func ()
|
|
{
|
|
local loc_var=23 # Declared as local variable.
|
|
echo # Uses the 'local' builtin.
|
|
echo "\"loc_var\" in function = $loc_var"
|
|
global_var=999 # Not declared as local.
|
|
# Therefore, defaults to global.
|
|
echo "\"global_var\" in function = $global_var"
|
|
}
|
|
|
|
func
|
|
|
|
# Now, to see if local variable "loc_var" exists outside the function.
|
|
|
|
echo
|
|
echo "\"loc_var\" outside function = $loc_var"
|
|
# $loc_var outside function =
|
|
# No, $loc_var not visible globally.
|
|
echo "\"global_var\" outside function = $global_var"
|
|
# $global_var outside function = 999
|
|
# $global_var is visible globally.
|
|
echo
|
|
|
|
exit 0
|
|
# In contrast to C, a Bash variable declared inside a function
|
|
#+ is local ONLY if declared as such.
|