Similar to other programming languages, variable in Bash are character string to which we assign a value.
Defining variables
To define a variable use the following syntax
variable_name=variable_value
Be awarded here you can’t put spaces around the assignment operator name = value
, because Bash uses spaces as a delimiter to separate command arguments.
Accessing variables
To access the value stored in a variable, prefix its name with the dollar sign ($)
VAR1="Zara Ali"
VAR2=100
echo $VAR1
echo $VAR2
Without a dollar sign the Bash will treat some special variable names as a Bash command.