Similar to other programming languages, shell programming also supports various types of operators to perform tasks. Operators can be categorized as follows:
Equal to an operator (=
) is the assignment operator that is used to initialize or change the value of a variable. This operator works on any data such as a string, integer, float, array, and so on. For example:
$ var=40 # Initializing variable var to integer value $ var="Hello" # Changing value of var to string value $ var=8.9 # Changing value of var to float value
Arithmetic operators are used for doing arithmetic operations on integers. They are as follows:
To perform any arithmetic operation, we prefix the expr
and let
keywords before the actual arithmetic expression. The following example shows how to perform an arithmetic operation in bash
:
#!/bin/bash #Filename: arithmetic.sh #Description: Arithmetic evaluation num1=10 num2=5 echo "Numbers are num1 = $num1 and num2 = $num2" echo "Addition = `expr $num1 + $num2`"`" echo "Subtraction = `expr $num1 - $num2`" echo "Multiplication = `expr $num1 * $num2`" echo "Division = `expr $num1 / $num2`" let "exponent = $num1 ** num2" echo "Exponentiation = $exponent" echo "Modulo = `expr $num1 % $num2`" let "num1 += $num2" echo "New num1 = $num1" let "num1 -= $num1" echo "New num2 = $num2"
The result after running this script will look as follows:
Numbers are num1 = 10 and num2 = 5 Addition = 15 Subtraction = 5 Multiplication = 50 Division = 2 Exponentiation = 100000 Modulo = 0 New num1 = 15 New num2 = 5
Logical operators are also known as Boolean operators. They are:
! (NOT), && (AND), and || (OR)
Performing a logical operation returns a Boolean value as true (1)
or false (0)
depending upon the values of variable(s) on which the operation is done.
One of the useful use-case is: suppose that we want to execute a command if the first command or operation returns successfully. In this case, we can use the &&
operator. Similarly, if we want to execute another command, irrespective of the first command that got executed or not, then we can use the || operator between two commands. We can use the ! operator to negate the true value. For example:
$ cd ~/Documents/ && ls
The cd
command is used to change the current path to the specified argument. Here, the cd ~/Documents/
command will change the directory to Documents if exists
. If it fails, then ls
won't get executed, but if cd
to Documents
succeeds, the ls
command will display the content of Documents directory
:
$ cat ~/file.txt || echo "Current Working directory $PWD" cat: /home/skumari/file.txt: No such file or directory Current Working directory /tmp/
The cat
command displays the content of file.txt
if it exists. Irrespective of the cat ~/file.txt
command execution, later the command that is echo "Current Working directory $PWD"
will be executed:
$ ! cd /tmp/foo && mkdir /tmp/foo bash: cd: /tmp/foo: No such file or directory
By running the preceding commands, first it will try to change the directory to /tmp/foo
. Here, ! cd /tmp/foo
means if change directory to /tmp/foo
doesn't succeed, then run the second command, which is mkdir /tmp/foo
. The mkdir
command is used to create a new directory. As a result of proceeding command execution, directory /tmp/foo
will be created if it doesn't exist.
$ cd /tmp/foo
Since the /tmp/foo
directory has been created, a successful change of the directory will occur.
Comparison operators compare two variables and check whether a condition is satisfied or not. They are different for integers and strings.
Comparison operators that are valid for integer variables (consider a
and b
as two integer variables; for example, a=20, b=35
) are as follows:
[ $a -eq $b ]
Comparison operators that are valid for string variables (consider a and b as two string variables; for example, a="Hello" b="World") are as follows:
[ $a = $b ]
Shell uses the <
and >
operators for redirection, so it should be used with an escape () if used under [ … ]. Double parentheses, (( ... )) or [[ … ]], doesn't need an escape sequence. Using [[ … ]] also supports pattern matching.
We will see the usage and examples of operators in more detail in Chapter 3, Effective Script Writing.
18.222.119.177