Bash Operator
In Bash, operators can be used for numerical calculations and comparisons. Below are some common operators along with their examples:
1. Arithmetic Operators
Arithmetic operators are primarily used to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and modulo. Here are some common arithmetic operators along with their examples:
+ # Addition
echo $((2 + 3)) # Output: 5
- # Subtraction
echo $((5 - 3)) # Output: 2
* # Multiplication
echo $((3 * 4)) # Output: 12
/ # Division
echo $((10 / 3)) # Output: 3
% # Modulo
echo $((10 % 3)) # Output: 1
2. Comparison Operators
Comparison operators are primarily used to compare the magnitude of two numerical values. Here are some common comparison operators along with their examples:
-eq # Equal
if [ 2 -eq 2 ]; then echo "2 equals 2"; fi # Output: 2 equals 2
-ne # Not equal
if [ 2 -ne 3 ]; then echo "2 does not equal 3"; fi # Output: 2 does not equal 3
-gt # Greater than
if [ 3 -gt 2 ]; then echo "3 is greater than 2"; fi # Output: 3 is greater than 2
-lt # Less than
if [ 2 -lt 3 ]; then echo "2 is less than 3"; fi # Output: 2 is less than 3
-ge # Greater than or equal
if [ 3 -ge 3 ]; then echo "3 is greater than or equal to 3"; fi # Output: 3 is greater than or equal to 3
-le # Less than or equal
if [ 2 -le 3 ]; then echo "2 is less than or equal to 3"; fi # Output: 2 is less than or equal to 3
3. Logical Operators
Logical operators are primarily used to combine conditional statements. Here are some common logical operators along with their examples:
! # Not
if ! [ 2 -eq 3 ]; then echo "2 does not equal 3"; fi # Output: 2 does not equal 3
-a # And
if [ 2 -eq 2 -a 3 -eq 3 ]; then echo "2 equals 2 and 3 equals 3"; fi # Output: 2 equals 2 and 3 equals 3
-o # Or
if [ 2 -eq 2 -o 3 -eq 4 ]; then echo "2 equals 2 or 3 equals 4"; fi # Output: 2 equals 2 or 3 equals 4
4. String Operators
String operators are primarily used to compare the content of strings. Here are some common string operators along with their examples:
= # Equal
if [ "hello" = "hello" ]; then echo "hello equals hello"; fi # Output: hello equals hello
!= # Not equal
if [ "hello" != "world" ]; then echo "hello does not equal world"; fi # Output: hello does not equal world
-z # Length is zero
if [ -z "" ]; then echo "the string is empty"; fi # Output: the string is empty
-n # Length is not zero
if [ -n "hello" ]; then echo "the string is not empty"; fi # Output: the string is not empty
5. File Test Operators
File test operators are primarily used to test the status of files. Here are some common file test operators along with their examples:
-e # File exists
if [ -e /etc/passwd ]; then echo "the file exists"; fi # Output: the file exists
-d # File is a directory
if [ -d /etc ]; then echo "the directory exists"; fi # Output: the directory exists
-f # File is a regular file
if [ -f /etc/passwd ]; then echo "the file is a regular file"; fi # Output: the file is a regular file
-r # File is readable
if [ -r /etc/passwd ]; then echo "the file is readable"; fi # Output: the file is readable
-w # File is writable
if [ -w /etc/passwd ]; then echo "the file is writable"; fi # Output: the file is writable
-x # File is executable
if [ -x /bin/bash ]; then echo "the file is executable"; fi # Output: the file is executable