Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX Shell basic operator


May 23, 2021 UNIX Getting started


Table of contents


Shell basic operator

Each shell supports a wide variety of operators. Our tutorial is based on the default Shell (Bourne), so all the important Bourne Shell operators are covered in our tutorial.

Here are the operators we'll discuss:

  • Arithmetic operator.
  • The relationship operator.
  • Boolean operator.
  • String operator.
  • The file test operator.

The original Bourne Shell did not have any mechanism to perform simple arithmetic operations, it used an external program awk or the simplest program expr.

Let's illustrate with a simple example that the two numbers add up:

    #!/bin/sh

    val=`expr 2 + 2`
    echo "Total value : $val"

This results in the following:

    Total value : 4

Note the following:

  • There must be a space between the operator and the expression, such as 2 plus 2 is incorrect, and should be written here as 2 plus 2.
  • The complete expression should be enclosed between two single quotes ''.

Arithmetic operator

The arithmetic operators supported by Bourne Shell are listed below.

Suppose variable a is assigned 10 and variable b is assigned 20:

Operator Describe Example
+ Addition - Adds the numbers on both sides of the operator `expr $a + $b` = 30
- Subtract - Subtract the number of operations on the left side of the operator by the number of operations on the right `expr $a - $b` = -10
* Multiplication - Multiplys the numbers on both sides of the operator `expr $a \* $b` = 200
/ Divide - Divide the number of operations to the left of the operator by the number of operations to the right `expr $b / $a` = 2
% Molding - Returns the remaining number by divided by the number of operations to the right by the number of operations to the left of the operator `expr $b % $a` = 0
= Assignment - Assigns the number of operations to the right of the operator to the number of operations to the left The value $b b is assigned to a
== Equal - Compare two numbers and, if the same, return true [ $a == $b ] = false
!= Not equal - Compare two numbers and, if different, return true. [ $a != $b ] = true

It is important to note here that all conditional expressions and operators must be separated by spaces, for example, $a $b is correct, and $a is not. $b. is incorrect.

All arithmetic calculations are performed for long integers.

The relationship operator

Bourne Shell supports the following relationship operators, which are specific to numeric data. They do not work on string values unless their values are numeric data.

For example, the following operator examines the relationship between 10 and 20 and the relationship between "10" and "20", but cannot be used to determine the relationship between "ten" and "twenty".

Suppose variable a is assigned 10 and variable b is assigned 20:

Operator Describe Example
-eq Check that the values of the two operasts are equal, and if the values are equal, the condition is true. [ $a -eq $b ] is not true.
-ne Check that the values of the two operasts are equal, and if the values are not equal, the condition is true. [ $a -ne $b ] is true.
-gt Check that the value of the left operance is greater than the value of the right operance, and if so, the condition is true. [ $a -gt $b ] is not true.
-lt Check that the value of the left operance is less than the value of the right operans, and if so, the condition is true. [ $a -lt $b ] is true.
-ge Check that the value of the left operance is greater than the value of the right operance, and if so, the condition is true. [ $a -ge $b ] is not true.
-le Check that the value of the left operance is less than or equal to the value of the right operans, and if so, the condition is true. [ $a -le $b ] is true.

It is important to note here that all conditional expressions and operators must be separated by spaces, for example, the $a $b is correct, and the $a $b is not correct.

Boolean operator

Bourne Shell supports the following Boolean operators.

Suppose variable a is assigned 10 and variable b is assigned 20:

Operator Describe Example
! This represents a logical negative. If the condition is false, return the true, and vice versa. [ ! false ] is true.
-o This represents the logical OR. If one of the action objects is true, the condition will be true. [ $a -lt 20 -o $b -gt 100 ] is true.
-a This represents logical AND. If both operating objects are true, the condition will be true, otherwise it will be false. [ $a -lt 20 -a $b -gt 100 ] is false.

String operator:

Bourne Shell supports the following string operators.

Suppose variable a is assigned "abc" and variable b is assigned "efg":

Example description

Operator Describe Example
= Check that the values of the two operasts are equal and, if so, that the condition is true. [ $a = $b ] is not true.
!= Check that the values of the two operasts are equal, and if the values are not equal, the condition is true. [ $a != $b ] is true.
-z Check that the length of a given string operastr is zero. If the length is zero, true is returned. [ -z $a ] is not true.
-n Check that the length of a given string operastr is not zero. True is returned if the length is not zero. [ -z $a ] is not false.
Str Check if the string str is a non-empty string. If it is an empty string, the false is returned. [ $a ] is not false.

File test operator:

The following operators are used to test the various properties associated with the Unix file.

Suppose a file variable file contains a file name "test" with a file size of 100 bytes with read, write, and execute permissions:

Example description

Operator Describe Example
-b file Check if the file is a special block, and if so, the condition is true. [ -b $file ] is false.
-c file Check if the file is a special character file, and if so, the condition becomes true. [ -c $file ] is false.
-d file Check if the file is a directory file and, if so, the condition is true. [ -d $file ] is not true.
-f file Check that the file is a normal file that is different from directory files and special files, and if so, the conditions are true. [ -f $file ] is true.
-g file Check if the file has a group ID (SGID) setting and, if so, the condition is true. [ -g $file ] is false.
-k file Check the file for paste bit settings, and if so, the condition is true. [ -k $file ] is false.
-p file Check if the file is a named pipe and, if so, the condition is true. [ -p $file ] is false.
-t file Check that the file descriptor is available and terminal-related, and if so, the condition is true. [ -t $file ] is false.
-u file Check the file for user id (SUID) settings, and if so, the condition is true. [ -u $file ] is false.
-r file Check that the file is readable and, if so, the condition is true. [ -r $file ] is true.
-w file Check if the file is writeable and, if so, the condition is true. [ -w $file ] is true.
-x file Check that the file is executable and, if so, the condition is true. [ -x $file ] is true.
-s file Check that the file size is greater than 0, and if so, the condition is true. [ -s $file ] is true.
-e file Check if the file exists. Even if the file is a directory directory, only exists, and the condition is true. [ -e $file ] is true.