In Unix, there are two types of shell variables:
- System variables: Created and maintained by Unix itself. This type of variables will be defined in CAPITAL LETTERS.
Example:
PATH, HOME etc..
- User defined variables: Created and maintained by user. This type of variables will be defined in lower letters.
Example:
a, sum etc..
Examples of Systems Variables
- HOME=/home/selva
- PATH=/usr/bin:/sbin:/bin:/usr/sbin
- SHELL=/bin/bash
- USERNAME=selva
You can print any of the above variables contains as follows:
- $ echo $USERNAME
- $ echo $HOME
Ex:
User Variables:
Ex:
$ num=10
To print the value of num echo $num
Ex:
- To define variable called ‘vech’ having value Bus:
$ vech=Bus
- To print contains of variable ‘n’ type command as follows:
- $ echo $num
- $ echo vech # prints ‘vech’ instead its value ‘Bus’
- $ echo num # prints ‘num’ instead its value ‘10’
- You must use $ followed by variable name.
Ex:
Variables containing more than one word:
- Often a variable has a value that contains space characters in it. If you try to set a variable containing spaces in the normal way, you will get an error message as follows.
- You should always use double quotes for the variables which has more than one word.
- $ NAME=Mike Ron
- sh: Ron: not found.
Ex:
- $ NAME=”Mike Ron”
- $ echo $NAME
- Mike Ron