Variable is like a container in which you store a value. There are two types of variables-
variable_name=value
When you assign a value to a variable. Remember one thing, there must be no space between the variable name, equal sign, and the value. If you forget to do so, you will get syntax error.
Note: Variables are case-sensitive, so variable Price
is different from the variable price
. Also, the variable name must not begin with a digit.
There are two ways of accessing the value stored in a variable-
Using dollar sign($)- To get a variable value, you prepend $ sign to a variable name i.e. $variable
. For example-
var1=50 var2=$var1 echo "The value of var1 is $var1" echo "The value of var2 is $var2"
Output of the above program
The value of var1 is 50 The value of var2 is 50
Using dollar and curly braces- To get a variable value, use ${variable}
. For example-
var1=50 var2=${var1} echo "The value of var1 is ${var1}" echo "The value of var2 is ${var2}"
Output of the above program
The value of var1 is 50 The value of var2 is 50