Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev
Chapter 4: Advanced Shell Scripting Commands
Next

Local and Global Shell variable (export command)

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

CommandMeaning
$ vech=BusCreate new local variable 'vech' with Bus as value in first shell
$ echo $vechPrint the contains of variable vech
$ /bin/bashNow load second shell in memory (Which ignores all old shell's variable)
$ echo $vechPrint the contains of variable vech
$ vech=CarCreate new local variable 'vech' with Car as value in second shell
$ echo $vechPrint the contains of variable vech
$ exitExit from second shell return to first shell
$ echo $vechPrint 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

CommandMeaning
$ vech=BusCreate new local variable 'vech' with Bus as value in first shell
$ echo $vechPrint the contains of variable vech
$ export vechExport first shells variable to second shell i.e. global varible
$ /bin/bashNow load second shell in memory (Old shell's variable is accessed from second shell, if they are exported )
$ echo $vechPrint the contains of variable vech
$ exitExit from second shell return to first shell
$ echo $vechPrint the contains of variable vech 


Prev
Home
Next
/dev/null - to send unwanted output of program
Up
Conditional execution i.e. && and ||