
In order to compare Strings in Bash Scripts you can make use of the = operator or using == operator,
Example 1: Using = operator% cat bash_example.sh
#!/bin/bash
#Bash Compare Strings
StringA="HelloWorld"
StringB="HelloWorld"
if [ "$StringA" = "$StringB" ]; then
echo "String A is equal to String B"
else
echo "String A is not equal to String B"
fi
Output:
String A is equal to String B
Example 1: Using == operator (pattern matching)#!/bin/bash
#Bash Compare Strings
StringX="Java"
StringY="PHP"
if [[ "$StringX" == "$StringY" ]]; then
echo "String X is equal to String Y"
else
echo "String X is not equal to String Y"
fi
Output:
String X is not equal to String Y
- How to Assign Bash Command to Variable
- How to Kill a port using bash terminal command?
- Bash For Loop Example
- bash: netstat: command not found
- Bash getopts Command Example
- How to fix bash ping command not found error
- [Fix] bash: script.sh: /bin/bash^M: bad interpreter: No such file or directory
- [fix] bash: ssh: command not found
- Fix bash: script.sh: Permission denied Error
- bash: ls command to see list files in current directory all subdirectories
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- How to use Autocomplete and Autosuggestion in Shell Commands
- Bash Hello World! Script Tutorial
- Install Bash Completion on macOS
- bash get year 2021 calendar
- How to create new user account in Windows bash
- Bash command to wait for seconds
- Know Bash shell version command
- How to start or open a new bourne-again shell (bash) session on Windows using Command Line CMD
- How to check your IP using bash for Windows?
- Bash Script to prefix files with sequential numbers in a directory
- How to Compare Strings in Bash
- Command to Sort File In Reverse Order [Unix/Linux/macOS]
- How to know the current shell you are logged in?
- List all Username and User ID using Bash Command
- How to reset AirPods or AirPods Pro using iPhone/iPad or iPod - iOS
- Installing Native Chrome Browser App on M1 Mac Device - Chrome
- How to update VIM version on a Mac - vi
- Change CKEditor Table Properties default width - CKEditor
- 12 August - International Youth Day celebrated worldwide - News
- Check macOS free disk space using Terminal command - MacOS
- pwd Command - Print Working Directory - Linux
- How to get list of all Java versions installed on macOS - Java