Shell Scripting Tutorial Variables Global and Local Variables
In Shell Scripting we will use Variables to store some values. Variables are more useful when you have repeated values you would like to use in script multiple times. Instead of writing same values multiple times in shell script we can assign one variable value and call same variable in script multiple times. In this article we are going to see Shell Scripting Tutorial Variables Global Variables and Local Variables.
Rules to Define Variables in Shell Scripting
- Variable Must be Start/Begin with Letter
- In Variable space are not allowed
- Instead of spaces we can make use of under_scores
- Special Characters Like @ # are not allowed
- Do not use Hyphen (-)
- Do not Start Variable with Number
- To Pass Commands to Variable use $(Command) Or `
Command
` - Define Local variable using local in front of variable (.i.e. local variable)
- Variable must be defined starting for the script, If not local variable.
- Variable are case sensitive
Let’s see few Examples about Variables Global and Local Variables
Below are few different types of variables defined as per above rules, Let’s see what happens when we execute below variable script.
#!/bin/bash ##Purpose: Define and execute Variable. Few Examples. ##Date: 19th Oct 2016 ##Author: Ankam Ravi Kumar ##WebSite: https://arkit.co.in ##Start ## GLOBAL VARIABLE A=10 B=22 HOST=$(hostname) HOST1=`hostname` 1value=222 false@var=false hyphen-a=wrong echo "Variable A Value: $A" echo "Variable B Vaule: $B" echo "Variable HOST value: $HOST" echo "Variable HOST1 value: $HOST1" echo "Wrong Variable 1value $1value" echo 'False @ Variable' $false@var echo "hyphen-a Variable Value: $hyphen-a" ##END
ravi@TechTutorials:/script$ sh variables.sh variables.sh: 14: variables.sh: 1value=222: not found variables.sh: 15: variables.sh: false@var=false: not found variables.sh: 16: variables.sh: hyphen-a=wrong: not found Variable A Value: 10 Variable B Vaule: 22 Variable HOST value: TechTutorials Variable HOST1 value: TechTutorials Wrong Variable 1value value False @ Variable @var hyphen-a Variable Value: -a
If you can see above shell script executed, It is showing an errors where variable rules are not matching.
14 1value=222 15 false@var=false 16 hyphen-a=wrong
Rule Says do to start variable with numbers, Do not use special symbols in variable and do not use hyphen -. So they are giving an errors.
Case sensitive Variable Examples
Whenever we are writing Variable we have to be careful if you write upper case and lower case each and every character will differ because it is case sensitive.
NOTE: Best practice is to write shell script variable in UPPER case.
#!/bin/bash ## Variable is Case sensitive ## Author: Ankam Ravi Kumar ## WebSite: https://arkit.co.in ## START NAME=Ravi NaMe=Kumar nAmE=arkit echo "NAME Variable: $NAME" echo "NaMe Variable: $NaMe" echo "nAmE Variable $nAmE" ## END
Executing script
ravi@TechTutorials:/script$ sh var1.sh
NAME Variable: Ravi
NaMe Variable: Kumar
nAmE Variable arkit
Shell Scripting Tutorial : Defining Local Variables
Local Variable will work only within function or within condition statement only. If your calling local variable from outside of function or outside of statement it will not work. Let’s see the example here.
ravi@TechTutorials:/script$ cat localvar.sh
#!/bin/bash
## Local Variable Example
## Date: 19th Oct 2016
## Author: Ankam Ravi Kumar
## Global Variable
VAR1=456
## Start
ravi ()
{
local VAR=123
echo "Local Var: $VAR"
}
ravi
echo "VAR Value $VAR"
echo "Global Variable: $VAR1"
## END
As per the above shell script code, i have defined one global variable called VAR1. Local Variable called VAR.
I am calling local and global variable after the function let’s see the output of it. Shell Scripting Tutorial
ravi@TechTutorials:/script$ sh localvar.sh Local Var: 123 VAR Value Global Variable: 456
Above script output does not printed variable VAR, because local variable can’t be called wherever we want.
Conclusion: Define Variable Carefully because if you mistakenly called variable instead one you want, whole the script execution will go false. Shell Scripting Tutorial
Related Articles
Shell Scripting Training Online
File system Usage Monitoring script
Create Multiple Users using single script
Thanks for your wonderful Support and Encouragement