Source From Here
Preface
When writing Bash scripts you will often need to compare two strings to check if they are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters.
This tutorial describes how to compare strings in Bash.
Comparison Operators
Comparison operators are operators that compare values and return true or false. When comparing strings in Bash you can use the following operators:
* string1 = string2 and string1 == string2
* string1 != string2
* string1 =~ regex
* string1 > string2
* string1 < string2
* -z string
* -n string
Following are a few points to be noted when comparing strings:
Check if Two Strings are Equal
In most cases, when comparing strings you would want to check whether the strings are equal or not.
The following script uses the if statement and the test [ command to check if the strings are equal or not with the = operator:
When the script is executed it will print the following output:
Here is another script that takes the input from the user and compares the given strings. In this example, we will use the [[ command and == operator.
Run the script and enter the strings when prompted:
You can also use the logical and && and or || to compare strings:
Output:
Check if a String Contains a Substring
There are multiple ways to check if a string contains a substring.
One approach is to use surround the substring with asterisk symbols * which means match all characters.
The script will echo the following:
Another option is to use the regex operator =~ as shown below:
The period followed by an asterisk .* matches zero or more occurrences any character except a newline character.
Check if a String is Empty
Quite often you will also need to check whether a variable is an empty string or not. You can do this by using the -n and -z operators.
Output:
Output:
Comparing Strings with the Case Operator
Instead of using the test operators you can also use the case statement to compare strings:
Output:
Lexicographic Comparison
The script will output the following:
When writing Bash scripts you will often need to compare two strings to check if they are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters.
This tutorial describes how to compare strings in Bash.
Comparison Operators
Comparison operators are operators that compare values and return true or false. When comparing strings in Bash you can use the following operators:
* string1 = string2 and string1 == string2
* string1 != string2
* string1 =~ regex
* string1 > string2
* string1 < string2
* -z string
* -n string
Following are a few points to be noted when comparing strings:
Check if Two Strings are Equal
In most cases, when comparing strings you would want to check whether the strings are equal or not.
The following script uses the if statement and the test [ command to check if the strings are equal or not with the = operator:
- #!/bin/bash
- VAR1="Linuxize"
- VAR2="Linuxize"
- if [ "$VAR1" = "$VAR2" ]; then
- echo "Strings are equal."
- else
- echo "Strings are not equal."
- fi
Here is another script that takes the input from the user and compares the given strings. In this example, we will use the [[ command and == operator.
- #!/bin/bash
- read -p "Enter first string: " VAR1
- read -p "Enter second string: " VAR2
- if [[ "$VAR1" == "$VAR2" ]]; then
- echo "Strings are equal."
- else
- echo "Strings are not equal."
- fi
You can also use the logical and && and or || to compare strings:
- [[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"
Check if a String Contains a Substring
There are multiple ways to check if a string contains a substring.
One approach is to use surround the substring with asterisk symbols * which means match all characters.
- #!/bin/bash
- VAR='GNU/Linux is an operating system'
- if [[ $VAR == *"Linux"* ]]; then
- echo "It's there."
- fi
Another option is to use the regex operator =~ as shown below:
- #!/bin/bash
- VAR='GNU/Linux is an operating system'
- if [[ $VAR =~ .*Linux.* ]]; then
- echo "It's there."
- fi
Check if a String is Empty
Quite often you will also need to check whether a variable is an empty string or not. You can do this by using the -n and -z operators.
- #!/bin/bash
- VAR=''
- if [[ -z $VAR ]]; then
- echo "String is empty."
- fi
- #!/bin/bash
- VAR='Linuxize'
- if [[ -n $VAR ]]; then
- echo "String is not empty."
- fi
Comparing Strings with the Case Operator
Instead of using the test operators you can also use the case statement to compare strings:
- #!/bin/bash
- VAR="Arch Linux"
- case $VAR in
- "Arch Linux")
- echo -n "Linuxize matched"
- ;;
- Fedora | CentOS)
- echo -n "Red Hat"
- ;;
- esac
Lexicographic Comparison
- #!/bin/bash
- VAR1="Linuxize"
- VAR2="Ubuntu"
- if [[ "$VAR1" > "$VAR2" ]]; then
- echo "${VAR1} is lexicographically greater then ${VAR2}."
- elif [[ "$VAR1" < "$VAR2" ]]; then
- echo "${VAR2} is lexicographically greater than ${VAR1}."
- else
- echo "Strings are equal"
- fi
沒有留言:
張貼留言