Variables
NarendraP
Learn How to Automate Common Tasks with Bash Shell Scripting
Introduction to Variables
Variables are useful to store data in shell scripts and Later we can use them if they required.
Simple Variable: x=4
Default value of a variable is Empty/Nothing
In Linux Shell Scripting, there are two types of variables:
System Variables:
Created and maintained by Operating System itself.
This type of variables are defined in CAPITAL LETTERS.
We can see them by using set command
Example: HOME, USER…
User Defined Variables:
Created and maintained by the user.
This type of variables are defined in lower letters.
But we can also take combination of upper and lower case letters.
Learn How to Automate Common Tasks with Bash Shell Scripting
Rules to Define User Defined Variables
Variable Name should contain only a-z or A-Z, 0-9 and _ characters.
Variable Name length should be less than or equal to 20 characters.
Variable Names are case sensitive. Means x and X are different.
Don’t Provide space on either sides of equal symbol while defining variables
Ex: x=4 is valid
x =4 or x = 4 or x= 4 are invalid
No need to declare variable type, Automatically it will take care while executing commands or scripts.
Use quotes for the data if data consist of spaces
We can store the output of a command into a variable as follows:
anyVariable=$(command)
anyVariable=`command`
We can assign one variable value/data into another using:
Name=“Shell Scripting”
NewName=$Name
NewName=${Name}
Learn How to Automate Common Tasks with Bash Shell Scripting
Thank you