Variables in Bash
The bash variables are the same as other programming languages. You don’t need to specify the type of variable in bash scripting. Read below example.
#!/bin/bash NAME="TecAdmin Tutorials" echo $NAME launchdate="Feb 08, 2013" echo $launchdate
Global vs Local Variables
Global variables are accessible anywhere in the script. Where Local variables are accessible in scope only. For example, a variable that is used inside a function only.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/bin/bash #Define bash global variable GLOBAL_VAR="global variable value" function hello { #Define bash local variable local LOCAL_VAR="local variable value" echo $LOCAL_VAR echo $GLOBAL_VAR ## This will accessible here } echo $LOCAL_VAR ## This will not accessible here echo $GLOBAL_VAR |
System Variables
System variables are responsible to define the aspects of the shell. These variables are maintained by bash itself. But we can modify these variables to change shell aspects.
Type env on bash shell to print all the available variables with there value.
$ env
Generally, these variables are defined in capital letters. For example PATH, SHELL, HOME, LANG, PWD and many more.