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.
If you can see above shell script executed, It is showing an errors where variable rules are not matching.
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.
No comments:
Post a Comment