Introduction to Unix Operators
Entities that enable operations between variables may be divided into different types, with a certain order of importance that occurs between them, and certain subcategories may also be present in each of these types. With a specific order of execution that exists for subcategories present in each form and each entity in a subcategory having its own special significance when dealing with the necessary number of variables. Operating with a logic that is unique to itself, and conforming to principles in mathematics, logic, or algorithms, in the sense of Unix programming, is referred to as Unix operators.
The Unix is made up of 3 parts: Kernel, shell, and programs.
- A Kernel is the core of Unix which manages time and memory for the tasks
- Shell is the CLI ( Command Line interface ) between the kernel and the user. When a user logs in to the system he enters the shell where it accepts commands and calls the respective program to process the same.
- Programs and Files – The various commands of Unix are documented in the manual which can be accessed by typing man <command>. There are also processes that run in the shell identified by their unique PID (process identifier). The files and directories stored inside Unix have a hierarchical structure/path starting with / meaning root location.
Types of Operators in Unix
There are 5 types of basic operators in Unix which are:
1. Arithmetic
2. Relational
3. Boolean
4. File Test Operators
5. Bitwise
1) Arithmetic Operators
These are used for performing arithmetic mathematical operations. Following are some of the arithmetic operators:
- Addition (+): Used to perform addition between 2 operands
Ex: c=`expr $a + $b` - Subtraction (-): Used to perform a subtraction between 2 operands
Ex: c=`expr $a – $b` - Multiplication (*): Used to multiply the value of 2 operands
Ex: c=`expr $a \* $b` - Division (/): Used to divide the first operand by the second one
Ex: c=`expr $a / $b` - Modulus (%): Used to provide remainder obtained by dividing the first operand by the second one
Ex: f=`expr $a % $b` - • Assignment (=): Used to assign the value given in the second operand to the first one
Ex: c=$b - Increment (++): Used to increment the value of the operand by 1.
Ex: ((a++)) – Post increment, ((++a)) – Pre increment, ((a–)) – Post decrement, ((–a)) – Pre decrement
2) Relational Operators
These are used to compare and find out the relation between 2 operands. Following are some of the relational operators:
- Equality (== or -eq): This returns true if both the operands are equal and false if not equal.
Ex: $a == $b - Non-Equality (!= or -ne): This is the opposite of equality operator where it returns true if both the operands are not equal and vice versa.
Ex: $a != $b - Greater than (> or -gt): This returns true if the first operand is greater than the second one and vice versa.
Ex: $a > $b - Lesser than (< or -lt): This returns true if the first operand is lesser than the second one and vice versa.
Ex: $a < $b - Greater than or Equal to (>= or -ge): This returns true if the first operand is greater than or equal to the second operand and false if not.
Ex: $a >= $b - Lesser than or Equal to (<= or -le): This returns true if the first operand is lesser than or equal to the second operand and false if not.
Ex: $a <= $b
3) Boolean/ Logical Operators
These are used to perform logical operators on the operands.
- Logical AND (&& or -a): This returns a Boolean true value if both operands satisfy true condition else returns false.
Ex: When a=20 and b=5 this [ $a -lt 10 -a $b -gt 1] becomes false since a is not less than 10 - Logical OR (|| or -o): This returns a Boolean true value if any one of the operands satisfies the condition else returns false.
Ex: When a=20 and b=5 this [ $a -lt 10 -o $b -gt 1s] becomes true since b greater than 1 is true - Not Equal to (!): This returns a Boolean true value if the operand value is false and vice versa.
Ex: If a=true [! $a == true] is false
4) File Operators
These are used to test the properties associated with the various files of the Unix filesystem.
- -b operator: This will be true when the file exists and is a special block file else will return false
- -c operator: This will be true when the file exists and is a character special file else will return false.
- -d operator: This will return true if the given filename is a directory else will return false.
- -e operator: This will return true if the given file exists else will return false.
- -g operator: This operator will return true if the given file’s SGID (Set Group ID) bit is set to true.
- -k operator: This will return true if the given file’s sticky bit is set to true.
- -r operator: This will return true if the given file is readable by the logged in user else will return false.
- -s operator: This checks the size of the given file and returns true if it is greater than zero else will return false.
- -u operator: This will return true if the given file has its SUID (Set User ID) bit set to true.
- -w operator: This will return true if the given file has to write access by the user else will return false.
- -x operator: This will check and return true if the given file can be executed by the user else will return false.
5) Bitwise Operators
These are used to perform bitwise operations on the operands.
- Bitwise AND (&): Here the AND operation is performed on each bit of the operand.
Ex: Consider a = 55 and b = 23 for all below examples
a & b = 01 - Bitwise OR (|): Here the OR operation is performed on each bit of the operand.
Ex: a | b = 77 - Bitwise XOR (^): Here the XOR operation is performed on each of operand.
Ex: a ^ b = 76 - Complement (~): This performs the complement on each bit of the operand and returns the value.
Ex: ~a = AA
Arithmetic operators can be used independently whereas the other types of operators need to be clubbed with conditional statements like if, switch statements to use its functionality.
This concludes the major types of operators in Unix along with their examples.
Recommended Articles
This is a guide to Unix Operators. Here we discuss the basic concept and types of operators in Unix which includes arithmetic, relational, boolean and bitwise in detail. You may also look at the following articles to learn more –
16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses