Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • Bash if String Comparison
  • Conclusion

Bash If Statements for String Comparison

PreviousBash if OptionsNextDebugging Techniques for Bash Scripts

Last updated 1 year ago

When writing bash scripts, we frequently compare strings. This enables us to develop conditions for if-then-else blocks based on the difference or similarities between the two strings. We can also perform lexicographic comparisons and check if the input string is empty or from which character it begins.

Bash if String Comparison

Bash has comparison operators which compare values of two operands and return either a true or a false. We can use these comparisons with the Bash if command. If command can be employed either with the arithmetic expansion (( .. )) or with the test alias [ .. ]. We need to be careful with the former as it might produce unexpected responses which we will discuss at the end of this article. For now we will perform all the comparisons using the square brackets [].

Strings equality

Bash doesn’t offer a built-in tool to verify if the two strings are equal like other programming languages. However, we can achieve the same using the if statement.

Example

var1="Football"var2="Football"var3="Hockey"if [ "$var1" = "$var2" ]; then echo true; else echo false; fiif [ "$var1" = "$var3" ]; then echo true; else echo false; fiif [ "$var1" = "Tennis" ]; then echo true; else echo false; fiif [ "Tennis" = "Tennis" ]; then echo true; else echo false; fiif [ "$var1" == "$var2" ]; then echo true; else echo false; fiif [ "$var1" == "$var3" ]; then echo true; else echo false; fiif [ $var1 == "Tennis" ]; then echo true; else echo false; fiif [ "Tennis" == "Tennis" ]; then echo true; else echo false; fi

We have three string variables, val1, val2 and val3. We can test the equality of two string variables, two string constants as well as a string variable and a constant using the if statement. The value of the variables can be compared by using either the single or double equal symbol (= or ==). It is a good practice to surround the variables with double quotes ("...").

Strings inequality

Just like we are checking for equality, we can do the same for inequality by doing the negation using the Not (!) operator.

Example

var1="Football"var2="Football"var3="Hockey"if [ "$var1" != "$var2" ]; then echo true; else echo false; fiif [ "$var1" != "$var3" ]; then echo true; else echo false; fiif [ "$var1" != "Tennis" ]; then echo true; else echo false; fiif [ "Tennis" != "Tennis" ]; then echo true; else echo false; fi

We again have three string variables, val1, val2 and val3. We can test the inequality of two string variables, two string constants as well as a string variable and a constant using the if statement. The value of the variables can be compared by using either the not equal symbol (!=).

String contains a substring

We can check if the string contains a substring using regular expressions.

Example

var1="Football"var2="Foot"if [[ "$var1" = *"$var2"* ]]; then echo true; else echo false; fiif [[ "$var1" =~ .*"$var2".* ]]; then echo true; else echo false; fi

We are using two methods to check if the given string contains a substring. We need to cover the substring with asterisk symbols (*). The asterisk symbol tells Bash to match all the characters before and after the letters "Foot". The other approach uses the regex operator =~ and the dot (.) with an asterisk. The regex matches zero or more occurrences of any character. The [[ is taken to execute the conditional command.

String starts with

Sometimes we want to read line-by-line and check if a string stored in a variable begins with some character. To achieve this, we simply need to follow the same approach of using the regex which we saw above.

Example

var1="Football"if [[ "$var1" = F* ]]; then echo true; else echo false; fiif [[ "$var1" = H* ]]; then echo true; else echo false; fi

We have a regex * to check if the string variable starts with "F" followed by any number of characters.:

Lexicographic

Lexicography is the activity of writing and editing dictionaries. In lexicographical comparison, two strings are compared alphabetically. This involves comparing the characters in a string sequentially from left to right.

Example

var1="Football"var2="Hockey"if [[ "$var1" > "$var2" ]]then echo $var1 is greaterelif [[ "$var1" < "$var2" ]]then echo $var2 is greater else echo $var1 and $var2 are equalfi

Lexicographical comparison involves comparing the characters in a string sequentially from left to right. The first letter of var1, F gets compared with var2, H. As H comes later than F, the second if condition will be executed.

Empty String

We often need to develop conditions to check if the string is empty or not. To achieve this, we can use the -n or -z option available with if command.

Example

var1="Football"var2=""if [ -z "$var1" ]; then echo true; else echo false; fiif [ -z "$var2" ]; then echo true; else echo false; fiif [ -n "$var2" ]; then echo false; else echo true; fiif [ ${#var2} -eq 0 ]; then echo true; else echo false; fi

The -n and -z option available with the if statement determines whether the string is empty or not. The alternate solution is to find the length of the variable holding the string. If it is zero, it's safe to say that the string is empty.

if-statement with ((...))

Let's see what happens if we use the arithmetic expansion for string comparisons:

Example

str1=astr2=bif (( $str1 == $str2 )); then echo true; else echo false; fi

Why are we getting the wrong result?

We are aware that (()) is an arithmetic construct and a string is taken as the name of a variable and the value of that variable is used. This happens after variables expand. Hence, Bash looks at the variables "a" and "b". Since both the variables are unset, both are considered zero. That is why they are equal and we get the result as true.

Conclusion

  • We often compare strings while writing Bash scripts.

  • We can apply the if commands on strings to compare them.

  • Regular expressions with string variables and constants inside the if condition help determine if the substring of a string exists.

  • We should not use (()) with if for string comparisons.

bash if string contains a substring
Strings equality
bash if strings inequality
bash if string starts with
Lexicographic
check empty string