Local variable only exists in current shell level/session.
Global variable can share value among different shell level/session
Both variables will be demolished after terminal closed, but can store as permanent in .bashrc
Refer to section <Save Variable as Permanent>
Check current level
echo $SHLVL
It will return 1, means at we at shell level 1
Create local variables
[Variable Name]=[Value]
Exp: color=blue
Verify variable
echo $[Variable Name]
Exp: echo $color
Open another session
bash
There's no new terminal will show up.
Check current level
echo $SHLVL
It will return 2, means at we at shell level 2, because open new session by using bash.
Verify variable
echo $[Variable Name]
Exp: echo $color
It will return nothing, because the variable only exists at shell level 1.
Exit the shell level 2
exit
Export the variable as global
export [Variable Name]
Exp: export color
Open another session
bash
There's no new terminal will show up.
Check current level
echo $SHLVL
It will return 2, means at we at shell level 2, because open new session by using bash.
Verify variable
echo $[Variable Name]
Exp: echo $color
It will return blue, because the variable becomes global now.
student@localhost:~$ echo $SHLVL
1
student@localhost:~$ color=blue
student@localhost:~$ echo $color
blue
student@localhost:~$ bash
student@localhost:~$ echo $SHLVL
2
student@localhost:~$ echo $color
student@localhost:~$ exit
exit
student@localhost:~$ export color
student@localhost:~$ bash
student@localhost:~$ echo $SHLVL
2
student@localhost:~$ echo $color
blue
student@localhost:~$
Direct to home directory
cd ~
Open .bashrc by using vim editor
vim .bashrc
Press Esc to make sure in Normal Mode.
Press I to enter insert mode and Enter to make sure in new line.
Create global variables
export [Variable Name]=[Value]
Exp: export color=blue
Press Esc to make sure in Normal Mode.
Save and exit
:wq --> Enter
Close and reopen terminal
Verify variable
echo $[Variable Name]
Exp: echo $color
Open another session
bash
There's no new terminal will show up.
Check current level
echo $SHLVL
It will return 2, means at we at shell level 2, because open new session by using bash.
Verify variable
echo $[Variable Name]
Exp: echo $color
It will return blue, because the variable becomes global now.