Conditional Statements
Conditional statements in Bash allow a script to make decisions based on conditions. These statements help in controlling the flow of execution by performing different actions based on whether a condition evaluates to true or false. Bash supports several types of conditional checks, including comparisons between numbers, strings, and file conditions.
Syntax
Bash conditional statements are typically written using if
, elif
, else
, and fi
keywords. The basic syntax is:
if [ condition ]; then
# Code to execute if condition is true
elif [ another_condition ]; then
# Code to execute if another_condition is true
else
# Code to execute if no conditions are met
fi
Basic Operators
The different operators that Bash provides can be classified into several categories based on the data type of the operands.
Numeric Operators:
Operator | Description |
---|---|
-eq |
Equal to |
-ne |
Not equal to |
-lt |
Less than |
-le |
Less than or equal to |
-gt |
Greater than |
-ge |
Greater than or equal to |
String Operators:
Operator | Description |
---|---|
= |
Equal to |
!= |
Not equal to |
-z |
String is empty |
-n |
String is not empty |
File Test Operators:
Operator | Description |
---|---|
-e |
File exists |
-f |
File is a regular file |
-d |
File is a directory |
-r |
File is readable |
-w |
File is writable |
-x |
File is executable |
Example
Here is an example that takes a number from the user and then uses conditional statements in Bash to check if the number is greater than, less than, or equal to 0:
#!/bin/bashread -p "Enter a number: " numif [ "$num" -gt 0 ]; thenecho "The number is positive."elif [ "$num" -lt 0 ]; thenecho "The number is negative."elseecho "The number is zero."fi
The above code produces the following output if the user inserts the number 5 when prompted:
Enter a number: 5The number is positive.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Command Line on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn the Command Line
Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.With CertificateBeginner Friendly4 hours