Comparison Operators
You can compare two variables in shell scripting. We do these comparisons to make decisions, we will see how to do that later in this article, but before that, here is a list of some comparison operators.
Integer comparison
Operator | Description |
---|---|
-eq | is equal to |
-ne | is not equal to |
-gt | is greater than |
-ge | is greater than or equal to |
-lt | is less than |
-le | is less than or equal to |
String Comparison
Operator | Description |
---|---|
== | is equal to |
!= | is not equal to |
\< | is less than, in ASCII alphabetical order |
\> | is greater than, in ASCII alphabetical order |
We add a \ before < and > because they need to be escaped when typed in the [ ] construct. Now, let’s see where these are used.
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup. You can list those commands and execute them all with just a single script. Let’s see how you can create a shell script and run it on Linux.