Introduction to Comparison Operators

Welcome to another exciting lesson in your shell scripting journey. In our previous lesson, we delved into using variables in shell scripts — learning how to store data that can be reused throughout a script. Today, we will build on that foundation by exploring Comparison Operators in shell scripting.

Comparison operators are essential tools for making decisions within your scripts. They allow you to compare values and execute different actions based on the results of these comparisons. This lesson will cover numeric and string comparisons and help you understand how to incorporate these comparisons into your scripts.

Let's dive in!

Explanation of Exit Status

Before diving into comparison operators, let's learn about exit statuses. An exit status is a numeric value returned by a command or a script to indicate its execution result. In the context of shell scripting, every command executed returns an exit status, which can be captured and used to determine the success or failure of that command.

An exit status of 0 indicates that the command or script was successful. A non-zero exit status (usually 1) indicates that the command or script failed.

In shell scripting, comparisons return an exit status to indicate the result of the comparison:

  • 0: Indicates the comparison is true.
  • 1: Indicates the comparison is false.

The echo $? command in shell scripting is used to print the exit status of the last executed command.

Numeric Comparisons
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal