
Bash Compare Strings
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
More Posts related to Bash,
- How to start or open a new bourne-again shell (bash) session on Windows using Command Line CMD
- How to create new user account in Windows bash
- Bash Hello World! Script Tutorial
- How to Compare Strings in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Command to Sort File In Reverse Order [Unix/Linux/macOS]
- Bash command to wait for seconds
- [fix] bash: ssh: command not found
- bash: netstat: command not found
- Know Bash shell version command
- List all Username and User ID using Bash Command
- How to use Autocomplete and Autosuggestion in Shell Commands
- How to know the current shell you are logged in?
- [Fix] bash: script.sh: /bin/bash^M: bad interpreter: No such file or directory
- 'pwd' is not recognized as an internal or external command, operable program or batch file. [Windows]
- How to Kill a port using bash terminal command?
- How to fix bash ping command not found error
- Install Bash Completion on macOS
- bash get year 2021 calendar
- How to check your IP using bash for Windows?
- Bash For Loop Example
- Fix bash: script.sh: Permission denied Error
More Posts:
- [Fix] Microsoft Windows OneDrive 0x8007018b Error Code - Windows
- Calculate Volume of Cylinder - C-Program
- [Solved] SharePoint Illegal operation attempted on a registry key that has been marked for deletion - SharePoint
- Calculate Area of Square - C-Program
- PowerShell git: The term git is not recognized as the name of a cmdlet - Microsoft
- Column Mode Editing in Notepad++ - NotepadPlusPlus
- How to List All Users in Linux - Linux
- Change Font Size in Visual Studio Code - HowTos