The need to compare strings in a Bash script is relatively common and can be used to check for certain conditions before proceeding on to the next part of a script. A string can be any sequence of characters. To test if two strings are the same, both strings must contain the exact same characters and in the same order. It could be a word or a whole sentence. For example, string one
is equal to string one
but is not equal to string two
. Get the idea?
In this guide, we’ll show you how to compare strings in the Bash shell on a Linux system. We’ll show this in the context of a simple if/else Bash script so you can see how testing for this condition would work when developing scripts, but we’ll also show how this same comparison can be done in the command line terminal.
In this tutorial you will learn:
- How to compare strings in Bash
- Example if/else Bash scripts that compare strings