Variables defined in a subshell are "local variables. Also variable operations inside a subshell, even to a GLOBAL variable do not affect the value of the variable outside the subshell! Kindly refer to the script given below:
#!/bin/bash
#Author: Parag Kalra
#Date: 1st June, 2008
#Name: glo-var-sub-shell.sh
#Usage: ./glo-var-sub-shell.sh
#Purpose: To illustrate concept of global variables within sub-shells
global_variable=
outer_num=12
echo "Outside Subshell"
echo "global_variable=$global_variable"
echo "outer_num=$outer_num "
echo " "
(
echo "Inside Subshell"
inner_variable=26
let "outer_num+=1"
echo "global_variable=$global_variable"
echo "outer_num=$outer_num"
echo "inner_variable=$inner_variable "
echo " "
)
echo "Again Outside of Subshell"
echo "outer_num=$outer_num"
echo "inner_variable=$inner_variable"
# END OF SCRIPT
The output of above script is:
Outside Subshell
global_variable=
outer_num=12
Inside Subshell
global_variable=
outer_num=13
inner_variable=26
Again Outside of Subshell
outer_num=12
inner_variable=
Just go through it and let me know if you happen to come across any bug in it. eMail me at paragkalra@gmail.com
Sunday, June 1, 2008
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment