In this section, we will cover the basic operators used in Bash scripting. Operators are essential for performing various operations on variables and values. We will explore arithmetic, relational, logical, and assignment operators.

Types of Operators

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators

  1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Description Example
+ Addition expr 3 + 2
- Subtraction expr 3 - 2
* Multiplication expr 3 \* 2
/ Division expr 6 / 2
% Modulus (remainder) expr 5 % 2

Example:

#!/bin/bash
# Arithmetic operations

a=10
b=5

sum=$((a + b))
diff=$((a - b))
prod=$((a * b))
quot=$((a / b))
mod=$((a % b))

echo "Sum: $sum"
echo "Difference: $diff"
echo "Product: $prod"
echo "Quotient: $quot"
echo "Modulus: $mod"

  1. Relational Operators

Relational operators are used to compare two values. They return true or false based on the comparison.

Operator Description Example
-eq Equal to [ $a -eq $b ]
-ne Not equal to [ $a -ne $b ]
-gt Greater than [ $a -gt $b ]
-lt Less than [ $a -lt $b ]
-ge Greater than or equal [ $a -ge $b ]
-le Less than or equal [ $a -le $b ]

Example:

#!/bin/bash
# Relational operations

a=10
b=5

if [ $a -eq $b ]; then
  echo "$a is equal to $b"
else
  echo "$a is not equal to $b"
fi

if [ $a -gt $b ]; then
  echo "$a is greater than $b"
else
  echo "$a is not greater than $b"
fi

  1. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Description Example
&& Logical AND [ $a -gt 0 ] && [ $b -gt 0 ]
` `
! Logical NOT [ ! $a -gt 0 ]

Example:

#!/bin/bash
# Logical operations

a=10
b=5

if [ $a -gt 0 ] && [ $b -gt 0 ]; then
  echo "Both $a and $b are greater than 0"
fi

if [ $a -gt 0 ] || [ $b -gt 0 ]; then
  echo "At least one of $a or $b is greater than 0"
fi

if [ ! $a -lt 0 ]; then
  echo "$a is not less than 0"
fi

  1. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Assign a=10
+= Add and assign a+=5
-= Subtract and assign a-=5
*= Multiply and assign a*=5
/= Divide and assign a/=5
%= Modulus and assign a%=5

Example:

#!/bin/bash
# Assignment operations

a=10
echo "Initial value: $a"

a+=5
echo "After a+=5: $a"

a-=3
echo "After a-=3: $a"

a*=2
echo "After a*=2: $a"

a/=4
echo "After a/=4: $a"

a%=3
echo "After a%=3: $a"

Practical Exercises

Exercise 1: Basic Arithmetic Operations

Write a script that takes two numbers as input and performs all arithmetic operations on them.

Solution:

#!/bin/bash
# Basic Arithmetic Operations

read -p "Enter first number: " num1
read -p "Enter second number: " num2

sum=$((num1 + num2))
diff=$((num1 - num2))
prod=$((num1 * num2))
quot=$((num1 / num2))
mod=$((num1 % num2))

echo "Sum: $sum"
echo "Difference: $diff"
echo "Product: $prod"
echo "Quotient: $quot"
echo "Modulus: $mod"

Exercise 2: Relational and Logical Operations

Write a script that takes two numbers as input and checks if both are positive, if at least one is positive, and if neither is positive.

Solution:

#!/bin/bash
# Relational and Logical Operations

read -p "Enter first number: " num1
read -p "Enter second number: " num2

if [ $num1 -gt 0 ] && [ $num2 -gt 0 ]; then
  echo "Both numbers are positive"
elif [ $num1 -gt 0 ] || [ $num2 -gt 0 ]; then
  echo "At least one number is positive"
else
  echo "Neither number is positive"
fi

Conclusion

In this section, we covered the basic operators in Bash, including arithmetic, relational, logical, and assignment operators. Understanding these operators is crucial for performing various operations in your scripts. Practice the exercises provided to reinforce your understanding and prepare for more advanced scripting topics.

© Copyright 2024. All rights reserved