• Shell Scripting
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question

Basic Operators in Shell Scripting

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

assignment operator in shell script

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

assignment operator in shell script

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.

assignment operator in shell script

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.

assignment operator in shell script

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.

assignment operator in shell script

Similar Reads

  • Basic Operators in Shell Scripting There are 5 basic operators in bash/shell scripting: Arithmetic OperatorsRelational OperatorsBoolean OperatorsBitwise OperatorsFile Test Operators1. Arithmetic Operators: These operators are used to perform normal arithmetics/mathematical operations. There are 7 arithmetic operators: Addition (+): B 7 min read
  • 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 Code #!/bin/sh str1=&quot;GeeksforGe 2 min read
  • Array Basics in Shell Scripting | Set 1 Consider a situation if we want to store 1000 numbers and perform operations on them. If we use a simple variable concept then we have to create 1000 variables and perform operations on them. But it is difficult to handle a large number of variables. So, it is good to store the same type of values i 6 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 - Creating a Binary file While working in Linux systems, we have used so many commands on a day-to-day basis. Most of the commands are in the binary format resides under /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, etc directories. As system administrators, we would have to write many shell scripts to do a few tasks or 4 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
  • Shell Scripting - Test Constructs Since 0 by UNIX tradition denotes "success," an if/then construct checks to see if the exit status of a list of commands is 0 and, if it is, runs one or more instructions. There is an exclusive command called [ (left bracket special character). It is a built-in synonym for test and is used for effic 6 min read
  • String Manipulation in Shell Scripting String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. Basics of pure bash string manipulation: 1. Assigning content 4 min read
  • Bash Scripting - While Loop A while loop is a statement that iterates over a block of code till the condition specified is evaluated to false. We can use this statement or loop in our program when do not know how many times the condition is going to evaluate to true before evaluating to false.   Table of Content The Syntax of 15+ 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
  • Bash Script - Read User Input Interacting with users is a crucial aspect of many Bash scripts. Whether prompting for information, selecting options, or confirming actions, reading user input is essential for building powerful and flexible scripts. This guide provides a comprehensive overview of reading user input in Bash, coveri 8 min read
  • Shell Script Examples For all the Linux distributions, the shell script is like a magic wand that automates the process, saves users time, and increases productivity. This shall scripting tutorial will introduce you to the 25 plus shall scripting examples. But before we move on to the topic of shell scripting examples, l 15+ min read
  • Shell Scripting - Functions and it's types Shell scripting is a powerful tool used to automate tasks in Unix-like operating systems. A shell serves as a command-line interpreter, and shell scripts often perform file manipulation, program execution, and text output. Here, we'll look into functions in shell scripting, exploring their structure 5 min read
  • Shell Scripting - Interactive and Non-Interactive Shell A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell 3 min read
  • Comments in Shell Script Comments are the useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. Comments are usually helpful to someone maintaining or enhancing your code when you are no longer around to answer questions about it 3 min read
  • Bash Scripting - Functions A Bash 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 reusability perspective, it is useful to store all of the inter-related commands for a specific task in a single file. We can 6 min read
  • Shell Scripting - Set Command The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg 7 min read
  • Shell Scripting - True Command A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix c 3 min read
  • Technical Scripter
  • Shell Script

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. assigning values to variables in shell script

    assignment operator in shell script

  2. Basic Operators in Shell Scripting

    assignment operator in shell script

  3. Basic Operators in Shell Scripting

    assignment operator in shell script

  4. Basic Operators in Shell Scripting

    assignment operator in shell script

  5. PowerShell Assignment Operators

    assignment operator in shell script

  6. Shell Script Operators

    assignment operator in shell script

VIDEO

  1. Java Script Logical Operator Lesson # 08

  2. Shell Assignment

  3. cs 305 4. Assignment: Advanced Shell Scripting

  4. CSC 109 Shell Script Assignment Demo

  5. 9th Assignment: Shell Scripting Basics

  6. 9th Assignment Shell Scripting Basics