Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 4: Advanced Shell Scripting Commands | ||
|
Normally all our variables are local. Local variable can be used in same shell, if you load another copy of shell (by typing the /bin/bash at the $ prompt) then new shell ignored all old shell's variable. For e.g. Consider following example
$ vech=Bus
$ echo $vech
Bus
$ /bin/bash
$ echo $vech
NOTE:-Empty line printed
$ vech=Car
$ echo $vech
Car
$ exit
$ echo $vech
Bus
Command | Meaning |
$ vech=Bus | Create new local variable 'vech' with Bus as value in first shell |
$ echo $vech | Print the contains of variable vech |
$ /bin/bash | Now load second shell in memory (Which ignores all old shell's variable) |
$ echo $vech | Print the contains of variable vech |
$ vech=Car | Create new local variable 'vech' with Car as value in second shell |
$ echo $vech | Print the contains of variable vech |
$ exit | Exit from second shell return to first shell |
$ echo $vech | Print the contains of variable vech (Now you can see first shells variable and its value) |
Global shell defined as:
"You can copy old shell's variable to new shell (i.e. first shells variable to seconds shell), such variable is know as Global Shell variable."
To set global varible you have to use export command.
Syntax:
export variable1, variable2,.....variableN
Examples:
$ vech=Bus
$ echo $vech
Bus
$ export vech
$ /bin/bash
$ echo $vech
Bus
$ exit
$ echo $vech
Bus
Command | Meaning |
$ vech=Bus | Create new local variable 'vech' with Bus as value in first shell |
$ echo $vech | Print the contains of variable vech |
$ export vech | Export first shells variable to second shell i.e. global varible |
$ /bin/bash | Now load second shell in memory (Old shell's variable is accessed from second shell, if they are exported ) |
$ echo $vech | Print the contains of variable vech |
$ exit | Exit from second shell return to first shell |
$ echo $vech | Print the contains of variable vech |
| ||
/dev/null - to send unwanted output of program | Conditional execution i.e. && and || |