Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linux
unix
command line

What are Unix/Linux basic operators?

Muhammad Ashir

Each shell supports various basic operations. The most popular shell is the Bourne Again Shell, or bash, which comes as a default with most Linux distributions.

There are five basic operations that one must know to use the bash shell:

  1. Arithmetic Operators
  2. Relational Operators
  3. Boolean Operators
  4. Bitwise Operators
  5. File Test Operators

Arithmetic operators

Bash supports the following arithmetic operators:

Operator

Description

Example

Addition

Adds the operands on either side

expr $a + $b

Subtraction

Subtracts the right hand operand from the left hand

expr $a - $b

Multiplication

Multiplies two operands

expr $a * $b

Division

Divides the right hand operand with the left hand operand and returns the quotient

expr $a / $b

Modulus

Divides the right hand operand with the left hand operand and returns the remainder

expr $a % $b

Increment

Unary operator to increment an operand by one

expr $((++a))

Decrement

Unary operator to decrement an operand by one

expr $((--a))

a=4
b=5
echo "a + b = $((a + b))"
echo "a - b = $((a - b))"
echo "a * b = $((a * b))"
echo "a / b = $((a / b))"
echo "a % b = $((a % b))"
echo "++a = $((++a))"
echo "--b = $((--b))"

Relational operators

Bash supports relational operators that work on variables with numeric values or strings that are numeric. Relational operators do not work on strings if their values are not numeric.

Relational operators either give true or false depending on the relation.

Operator

Description

Example

==

Compares two operands and returns true if they are equal; otherwise, it returns false

[$a == $b]

!=

Compares two operands and returns true if they are not equal; otherwise, it returns false

[$a != $b]

<

Less than operator; returns true if the left hand operand is less than the right hand operand

[$a < $b]

<=

Less than or equal to operator; returns true if the left hand operand is less than or equal to the right hand operand

[$a <= $b]

>

Greater than operator; returns true if the left hand operand is greater than the right hand operand

[$a > $b]

>=

Greater than or equal to operator; returns true if the left hand operand is greater than or equal to the right hand operand

[$a >= $b]

a=100
b=10

if(( $a == $b ))
then
  echo "a and b are equal"
fi

if(( $a != $b ))
then
  echo "a and b are not equal"
fi

if(( $a > $b ))
then
  echo "a is greater than b"
else
  echo "a is not greater than b"
fi

if(( $a >= $b ))
then
  echo "a is greater or equal to than b"
else
  echo "a is not greater or equal to than b"
fi

Boolean operators

The following boolean operators are supported by bash:

Operator

Description

Example

!

The logical negation operator

[ ! false ]

-o ( || )

The logical OR operator

[ $a < 20 || $b > 30 ]

-a ( && )

The logical AND operator

[ $a < 20 & $b > 30 ]

echo "LOGICAL_AND = $((1 && 0))"
echo "LOGICAL_OR  = $((0 || 1))"
echo "LOGICAL_Neg = $((!0))"

Bitwise operators

Bitwise operators are used to perform bitwise operations on bit fields.

Operators

Description

Example

&

Performs the binary AND operation bit by bit on the arguments

echo "$((0x4 & 0x1))"

|

Performs the binary OR operation bit by bit on the arguments

echo "$((0x4 | 0x1))"

^

Performs the binary XOR operation bit by bit on the arguments

echo "$((0x4 ^ 0x1))"

~

Performs the binary NOT operation bit by bit on the arguments

echo "$((~0x4))"

<<

Shifts the bit field to the left by the number of times specified by the right hand operand

echo "$((0x4 << 1))"

>>

Shifts the bit field to the right by the number of times specified by the right hand operand

echo "$((0x4 >> 1))"

echo "0x4 & 0x0 = $((0x4 & 0x0))"
echo "0x4 | 0x1 = $((0x4 | 0x1))"
echo "0x1 ^ 0x1 = $((0x1 ^ 0x1))"
echo "0x1 << 4 = $((0x1 << 4))"
echo "0x4 >> 2 = $((0x4 >> 2))"

File test operators

Bash provides operators to test for various properties of files; these operators are known as file test operators.

Operator

Description

Example

-b

Checks whether a file is a block special file or not

[ -b $FileName]

-c

Checks whether a file is character special or not

[ -c $FileName]

-d

Checks if a given directory exists or not

[ -d $FileName]

-e

Checks whether a given file exists or not

[ -e $FileName]

-r

Checks if the given file has read access or not

[ -r $FileName]

-w

Checks if the given file has write access or not

[ -w $FileName]

-x

Checks if the given file has execute access or not

[ -x $FileName]

-s

Checks the size of the given file

[ -s $FileName]

main.sh
test.txt
read -p "Enter filename " FileName

if [ -e $FileName ]
then
  echo "$FileName exists"
  if [ -r $FileName ]
  then
    echo "$FileName has read access"
  else 
    echo "$FileName does not have read access!"
  fi
  if [ -w $FileName ]
  then
    echo "$FileName has write access"
  else 
    echo "$FileName does not have write access!"
  fi
  if [ -x $FileName ]
  then
    echo "$FileName has execute access"
  else 
    echo "$FileName does not have execute access!"
  fi

  if [ -s $FileName ]
  then
    echo "$FileName size is non-zero"
  else 
    echo "$FileName is empty"
  fi

else 
  echo "$FileName not found!"
fi

Enter the input below

RELATED TAGS

linux
unix
command line

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring