Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Basic Operators in Shell Scripting
Next article icon

Basic Operators in Shell Scripting

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

There are 5 basic operators in bash/shell scripting:

  • Arithmetic Operators
  • Relational Operators
  • Boolean Operators
  • Bitwise Operators
  • File Test Operators

1. Arithmetic Operators: These operators are used to perform normal arithmetics/mathematical operations. There are 7 arithmetic operators:

  • Addition (+): Binary operation used to add two operands.
  • Subtraction (-): Binary operation used to subtract two operands.
  • Multiplication (*): Binary operation used to multiply two operands.
  • Division (/): Binary operation used to divide two operands.
  • Modulus (%): Binary operation used to find remainder of two operands.
  • Increment Operator (++): Unary operator used to increase the value of operand by one.
  • Decrement Operator (- -): Unary operator used to decrease the value of a operand by one
C
#!/bin/bash  # reading data from the user read -r -p "Enter a: " a  read -r -p "Enter b: " b  add=$((a+b)) echo "Addition of a and b are: "${add}  sub=$((a-b)) echo "Subtraction of a and b are: "${sub}  mul=$((a*b)) echo "Multiplication of a and b are: "${mul}  div=$((a/b)) echo "Division of a and b are: "${div}  mod=$((a%b)) echo "Modulis of a and b are: "${mod}  ((++a)) echo "Increment operator when applied on $a results into a :" "${a}"  ((--b)) echo "Decrement operator when applied on 'b' results into b :" "${b}" 

Output:


2. Relational Operators: Relational operators are those operators which define the relation between two operands. They give either true or false depending upon the relation. They are of 6 types:

  • '==' Operator: Double equal to operator compares the two operands. Its returns true is they are equal otherwise returns false.
  • '!=' Operator: Not Equal to operator return true if the two operands are not equal otherwise it returns false.
  • '<' Operator: Less than operator returns true if first operand is less than second operand otherwise returns false.
  • '<=' Operator: Less than or equal to operator returns true if first operand is less than or equal to second operand otherwise returns false
  • '>' Operator: Greater than operator return true if the first operand is greater than the second operand otherwise return false.
  • '>=' Operator: Greater than or equal to operator returns true if first operand is greater than or equal to second operand otherwise returns false
C
#!/bin/bash   #reading data from the user read -p 'Enter a : ' a read -p 'Enter b : ' b   if(( $a==$b )) then     echo a is equal to b. else     echo a is not equal to b. fi   if(( $a!=$b )) then     echo a is not equal to b. else     echo a is equal to b. fi   if(( $a<$b )) then     echo a is less than b. else     echo a is not less than b. fi   if(( $a<=$b )) then     echo a is less than or equal to b. else     echo a is not less than or equal to b. 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 than or equal to b. else     echo a is not greater than or equal to b. fi 

Output:

3. Logical Operators : They are also known as boolean operators. These are used to perform logical operations. They are of 3 types: 

  • Logical AND (&&): This is a binary operator, which returns true if both the operands are true otherwise returns false.
  • Logical OR (||): This is a binary operator, which returns true if either of the operands is true or if both the operands are true. It returns false only if both operands are false.
  • Not Equal to (!): This is a unary operator which returns true if the operand is false and returns false if the operand is true.
C
#!/bin/bash  #reading data from the user read -p 'Enter a : ' a read -p 'Enter b : ' b  if(($a == "true" & $b == "true" )) then     echo Both are true. else     echo Both are not true. fi  if(($a == "true" || $b == "true" )) then     echo Atleast one of them is true. else     echo None of them is true. fi  if(( ! $a == "true"  )) then     echo "a" was initially false. else      echo "a" was initially true.  fi 

Output:

4. Bitwise Operators: A bitwise operator is an operator used to perform bitwise operations on bit patterns. They are of 6 types:

  • Bitwise And (&): Bitwise & operator performs binary AND operation bit by bit on the operands.
  • Bitwise OR (|): Bitwise | operator performs binary OR operation bit by bit on the operands.
  • Bitwise XOR (^): Bitwise ^ operator performs binary XOR operation bit by bit on the operands.
  • Bitwise complement (~): Bitwise ~ operator performs binary NOT operation bit by bit on the operand.
  • Left Shift (<<): This operator shifts the bits of the left operand to left by number of times specified by right operand.
  • Right Shift (>>): This operator shifts the bits of the left operand to right by number of times specified by right operand.
C
#!/bin/bash  #reading data from the user read -p 'Enter a : ' a read -p 'Enter b : ' b  bitwiseAND=$(( a&b )) echo Bitwise AND of a and b is $bitwiseAND  bitwiseOR=$(( a|b )) echo Bitwise OR of a and b is $bitwiseOR  bitwiseXOR=$(( a^b )) echo Bitwise XOR of a and b is $bitwiseXOR  bitiwiseComplement=$(( ~a )) echo Bitwise Compliment of a is $bitiwiseComplement  leftshift=$(( a<<1 )) echo Left Shift of a is $leftshift  rightshift=$(( b>>1 )) echo Right Shift of b is $rightshift 

Output:


5. File Test Operator: These operators are used to test a particular property of a file.

  • -b operator: This operator check whether a file is a block special file or not. It returns true if the file is a block special file otherwise false.
  • -c operator: This operator checks whether a file is a character special file or not. It returns true if it is a character special file otherwise false.
  • -d operator: This operator checks if the given directory exists or not. If it exists then operators returns true otherwise false.
  • -e operator: This operator checks whether the given file exists or not. If it exits this operator returns true otherwise false.
  • -r operator: This operator checks whether the given file has read access or not. If it has read access then it returns true otherwise false.
  • -w operator: This operator check whether the given file has write access or not. If it has write then it returns true otherwise false.
  • -x operator: This operator check whether the given file has execute access or not. If it has execute access then it returns true otherwise false.
  • -s operator: This operator checks the size of the given file. If the size of given file is greater than 0 then it returns true otherwise it is false.
C
#!/bin/bash  #reading data from the user read -p 'Enter file name : ' FileName  if [ -e $FileName ] then     echo File Exist else     echo File doesnot exist fi  if [ -s $FileName ] then     echo The given file is not empty. else     echo The given file is empty. fi  if [ -r $FileName ] then     echo The given file has read access. else     echo The given file does not has read access. fi  if [ -w $FileName ] then     echo The given file has write access. else     echo The given file does not has write access. fi  if [ -x $FileName ] then     echo The given file has execute access. else     echo The given file does not has execute access. fi 

Output:


Next Article
Basic Operators in Shell Scripting

D

DrRoot_
Improve
Article Tags :
  • Technical Scripter
  • Linux-Unix
  • Shell Script

Similar Reads

    Shell Scripting - Define #!/bin/bash
    A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A
    3 min read
    String Operators | Shell Script
    Pre-Requisite: Conditional Statement in Shell Script There are many operators in Shell Script some of them are discussed based on string. Equal operator (=): This operator is used to check whether two strings are equal. Syntax:Operands1 = Operand2Example: php #!/bin/sh str1="GeeksforGeeks"; str2="ge
    2 min read
    Shell Script to Perform Operations on a File
    Most of the time, we use shell scripting to interact with the files. Shell scripting offers some operators as well as some commands to check and perform different properties and functionalities associated with the file. For our convenience, we create a file named 'geeks.txt' and another .sh file (or
    5 min read
    Shell Scripting - Decision Making
    A Shell script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can
    7 min read
    Basics of Batch Scripting
    Batch Scripting consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. It is not commonly used as a programming language and so it is not commonly practiced and is not trending but its control and dominance in the Windows environment can never b
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences