Shell Scripting Tutorial – Shell Variables

The variable is place holder for storing data. The value assigned could be a number, text, filename, device etc. A variable is actually a pointer to the actual data. The shell provides functionality to create, assign, and delete variables.

Shell variables can be used at the command line terminal. It is similar to other programming language variables, shell variables do have some different characteristics such as:

  • Shell variables do not need to be declared.
  • All shell variables are of type string.

There are two types of variables.

  • User defined variables
  • System variable

User defined variables

How to assign value to a variable

You can assign any value to a variable. The basic syntax as follows,

variable-name=value

Value is assigned to ‘variable name’ and value should be on right side of equal (= sign).

$ number=10

# this is how we assigned variables
$10=number

#  Error , This is not the way we assigned values to a variable. 
#  value should be on right side of = sign.

To define variable called ‘os’ having value unix.

$ os=unix

To define variable called n having value 10.

$ n=10

Rules for Assigning Variable Name (User Defined and System variable)

There are few rules to define a variable in unix shell scripting as follows,

  • While assigning name of the variable must start with Alphanumeric character or underscore character (_) followed by one or more alphanumeric character. For instance, below are few valid shell variables.
HOME
SYSTEM_VERSION
variable_name
  • Do not put spaces on either side of the equal sign while assigning values to variable. For instance, in the following variable declaration there will be no error.
$ no=10

Below is not a correct way of variable declaration,

$ no =10
$ no= 10
$ no = 10
  • Variables are case-sensitive, just like filename in Linux.
$ no=10
$ No=11
$ NO=20
$ nO=2

Here all variable names are different so to print value 20 you have to use $ echo $NO as follows,

$ echo $no
# will print 10 but not 20
$ echo $No 
# will print 11 but not 20
$ echo $nO 
# will print 2 but not 20
  • NULL variable can be defined as follows (NULL variable is variable which has no value).
$ vech=
$ vech=""

Next, try to print it’s value by following command,

$ echo $vech

Here, you will get nothing as variable has no value i.e. NULL variable.

  • Do not use ?,* etc, to name your variable names.

How to access the value of variable

To access the value of a variable $ symbol can be used. 

$ a=10
$ echo $a
10

Variable are not declared

If you try to access the variable which is not declared yet, the blank line will appear in terminal. Means the value of that particular variable is null or not assigned.

$ echo $b

Capture output of a command placed inside a variable

You can use back quote (`) (above the tab key on keyboard) to get the output of a command in a variable,

$ user=`whoami`
$ echo $user
ravi

There is another way to get the output of a command to a variable,

$ user=$(whoami`)
$ echo $user
ravi

Arithmetic on variables

As we said earlier, all shell variables are of type string. As you can’t do arithmetic on strings so this might lead one to conclude that arithmetic using shell variables is not possible. Not so fast!. In general, expr is used as follows:

expr operand1 operator operand2

Note: The spaces on both sides of the operator are mandatory. Some of the possible operators include:

  • addition +
  • subtraction –
  • multiplication *   # must be written with \ before the * (See the example below)
  • division /
  • modulus %

Therefore arithmetic operations can be performed as follows,

$ expr 10 + 2 [Enter] #adding 10 + 2
12

$ I=10

$ expr $I + 2 [Enter]       # same using a variable
12

$ expr 10 \* 2 [Enter]     # multiplying by 2
20

We have an alternative way to perform arithmetic operations available in some of the newer shells (e.g. bash, ksh93). The syntax as follows,

$((expression))

$ echo $((10 + 2)) [Enter]   # with spaces around operator
12

$ echo $((10+2)) [Enter]     # without spaces
12

$ X=10 [Enter]

$ echo $((X + 2)) [Enter]     # note no $ on X
12

System Variables

System variables generally created and maintained by Linux/Unix itself. These type of variable usually defined in CAPITAL LETTERS. Some of the commonly used variables are as follows. 

PS1                      -       Command prompt
HOME                -       Your home directory
PWD                   -       Your present working directory
LOGNAME       -       Your login name
USER                  -       Same as login name
HISTSIZE         -       Number of executed commands to be stored in history file
HOSTNAME    -       Your host name
SHELL               -       Your login shell
PPID                   -       Process id of parent process

PATH Variable

PATH variable are nothing but environment variables. When we type any command on prompt checks the executable in path of the system for execution of the command. If shell doesn’t get the path then it displays error as command not found. See how to set a path variable.

$ echo $PATH

/lib/qt3/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/home/ravi/bin:/bin

The above command will displays the path of your system observe the bin is present in current path. 

Set your PATH

You can set PATH temporarily as follows,

$ export PATH=$PATH:/new/path

If your computer got restarted or you create a new terminal, your temporarily path is gone! This is by default. The variable $PATH is set by your shell every time it launches but you can set it permanently. For Bash shell, you need to simply add the line from above (export PATH=$PATH:/new/path) to the appropriate file that will be read when your shell launches. .bashrc is a good choice to export the PATH . This is a hidden file so please put . (dot) before.

$ vim .bashrc

export PATH=$PATH:/new/path

Read Also : Shell Scripting – Basic Concepts of Shell Scripts

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

Leave a Comment