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 = Operand2
Example:
#!/bin/shstr1="GeeksforGeeks";str2="geeks";if[$str1=$str2]thenecho"Both string are same";elseecho"Both string are not same";fichevron_rightfilter_noneOutput:
Both string are not same
- Not Equal operator (!=): This operator is used when both operands are not equal.
Syntax:
Operands1 != Operands2
Example:
#!/bin/shstr1="GeeksforGeeks";str2="geeks";if[$str1!=$str2]thenecho"Both string are not same";elseecho"Both string are same";fichevron_rightfilter_noneOutput:
Both string are not same
- Less then (\<): It is a conditional operator and used to check operand1 is less then operand2.
Syntax:
Operand1 \< Operand2Example:
#!/bin/shstr1="GeeksforGeeks";str2="Geeks";if[$str1\<$str2]thenecho"$str1 is less then $str2";elseecho"$str1 is not less then $str2";fichevron_rightfilter_noneOutput:
GeeksforGeeks is not less then Geeks
- Greater then (\>): This operator is used to check the operand1 is greater then operand2.
Syntax:
Operand1 \> Operand2Example:
#!/bin/shstr1="GeeksforGeeks";str2="Geeks";if[$str1\>$str2]thenecho"$str1 is greater then $str2";elseecho"$str1 is less then $str2";fichevron_rightfilter_noneOutput:
GeeksforGeeks is greater then Geeks
- Check string length greater then 0: This operator is used to check the string is not empty.
Syntax:
[ -n Operand ]
Example:
#!/bin/shstr="GeeksforGeeks";if[ -n$str]thenecho"String is not empty";elseecho"String is empty";fichevron_rightfilter_noneOutput:
String is not empty
- Check string length equal to 0: This operator is used to check the string is empty.
Syntax:
[ -z Operand ]
Example:
#!/bin/shstr="";if[ -z$str]thenecho"String is empty";elseecho"String is not empty";fichevron_rightfilter_noneOutput:
String is empty
Recommended Posts:
- Introduction to Linux Shell and Shell Scripting
- Bash shell script to find sum of digits
- Bash shell script to swap two numbers
- Looping Statements | Shell Script
- Conditional Statements | Shell Script
- Implementing Directory Management using Shell Script
- Bash shell script to find out the largest value from given command line arguments
- Automated Recursive Encryption in a Directory Using Shell Script
- Basic Operators in Shell Scripting
- Reverse a String | Shell Programming
- How to find time taken by a command/program on Linux Shell?
- Making your own Linux Shell in C
- A Shell program To Find The GCD | Linux
- Array Basics Shell Scripting | Set 2 (Using Loops)
- Array Basics in Shell Scripting | Set 1
- Developing a Linux based shell
- Basic Shell Commands in Linux
- How to check if a directory or a file exists in system or not using Shell Scripting?
- Create a password generator using shell scripting
- How to Install and Configure Fish Shell in Ubuntu?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

