KandZ – Tuts

We like to help…!

Linux CLI 68 🐧 operators in shell scripts

a - unary, assignment and logical operators in shell scripts
Shell scripts use various operators to perform operations on variables, strings, numbers, and other data types.
Common categories of operators used in shell scripts:
Unary, Arithmetic, Relational, Logical, assignment, file test , string and bit operators
Unary refers to the number of operands, so they take one operand.
There are two unary operators + and - which indicate whether a number is positive or negativ
Assignment operators are:
= → simple assignment
+=, -=, *=, /=, %= → compound assignments
Logical Operators:
&& → logical AND
|| → logical OR
! → logical NOT

b - Arithmetic relational and string operators in shell scripts
Arithmetic Operators:
→ addition
→ subtraction
* → multiplication
/ → division (integer division only)
% → modulus (remainder of division)
++ → increment
-- → decrement
Relational Operators:
-lt or → less than
-gt or → greater than
-le or = → less than or equal to
-ge or = → greater than or equal to
-eq or == → equal to (in arithmetic context)
-ne or!= → not equal to
String Operators:
= or == → equal to
!= → not equal to
→ greater than (in ASCII order)
→ less than (in ASCII order)

c - file test and bit operators in shell scripts
File Test Operators:
-e → file exists
-f → file is a regular file
-d → directory exists
-r → file is readable
-w → file is writable
-x → file is executable
-s → file has non-zero size
Bitwise operators are used to perform operations on individual bits of a number in shell scripting.
common bitwise operators:
& → bitwise AND
| → bitwise OR
^ → bitwise XOR (exclusive OR)
~ → bitwise NOT (negation)
→ left shift
→ right shift

Leave a Reply