Here we will learn to create variables in shell script. Variable are used to hold some values which can be used in other parts of script.
Types of Variable
There are two type of variable used in Shell scripting.
- User Defined Variable (UDV)
- System Variable
Syntax for creating variable
You need to use “=” sign to create variable.
Example of creating variable
firstName=Vivekanand
firstName=”Vivekanand Gautam”
Naming Convention for variable
- Names must start with a letter.
- A name must not contain embedded spaces. Underscores can be used instead.
- Punctuation marks can not be used.
Rules Related to Variable (UDV and System Variable)
- You can not have spaces between = and variable name and / or value. e.g firstName=Vivekanand (Valid Syntax)
firstName =Vivekanand (Invalid Syntax)
firstName= Vivekanand (Invalid Syntax) - Variable Name should be on left sign of equal sign(=) and variable value should be on right side of equal sign.
- Variables are case sensitive. no and No are different in terms of variable names.
- Do not use * and ?.. in variable names.
- NULL value is treated as No Value. If you want to define variable with NULL value then define variable without any value or empty spaces. e.g fname=
fname=””
If you tries to print value for these variable nothing will be printed on screen.