if else in shell script example

Mastering 'if else' in Shell Script: Practical Examples

By Amanda Jackson
AD

In the realm of shell scripting, the if-else construct is a fundamental building block, enabling scripts to make decisions based on certain conditions. This article explores the usage of if-else in shell scripts, complete with examples to illustrate its functionality.

Before delving into the intricacies of if-else statements, let's briefly recap the basic syntax. The general structure is as follows:

```bash if condition then command1 command2 ... else command3 command4 ... fi ```

Understanding the Basic If-Else Syntax

The if keyword is followed by a condition enclosed in square brackets. If the condition evaluates to true, the commands following the then keyword are executed. If the condition is false, the commands after the else keyword are executed. The fi keyword marks the end of the if-else statement.

AD
Bash if elif else Statement with Examples - Linux Shell Scripting Guide
Bash if elif else Statement with Examples - Linux Shell Scripting Guide
Learn Interactive Shell Scripting in Linux
Learn Interactive Shell Scripting in Linux
Master Shell Input with read
Master Shell Input with read

Now, let's dive into some examples to solidify your understanding.

Example 1: Simple If-Else Statement

Here's a simple example that checks if a user-supplied number is positive, negative, or zero:

40 Simple Yet Effective Linux Shell and Bash Script Examples
40 Simple Yet Effective Linux Shell and Bash Script Examples
How to Create/Write a Simple/Sample Linux Shell/Bash Script
How to Create/Write a Simple/Sample Linux Shell/Bash Script
40 Simple Yet Effective Linux Shell and Bash Script Examples
40 Simple Yet Effective Linux Shell and Bash Script Examples
```bash read -p "Enter a number: " num if [ $num -gt 0 ] then echo "The number is positive." elif [ $num -lt 0 ] then echo "The number is negative." else echo "The number is zero." fi ```

In this script, the user is prompted to enter a number. The if-else statement then checks the value and prints an appropriate message.

Example 2: If-Else with String Comparison

Shell scripts can also compare strings using the = operator. Here's an example:

AD
```bash read -p "Enter your name: " name if [ "$name" = "John Doe" ] then echo "Hello, $name!" else echo "You are not John Doe." fi ```

In this case, the script greets the user if their name is "John Doe"; otherwise, it informs them that they are not John Doe.

Bash Quoting Rules Explained with Practical Examples
Bash Quoting Rules Explained with Practical Examples
How to Perform Syntax Checking Debugging Mode in Shell Scripts
How to Perform Syntax Checking Debugging Mode in Shell Scripts
Linux Shell Scripting Command Cheat Sheet
Linux Shell Scripting Command Cheat Sheet
Linux Shell Guide
Linux Shell Guide
Bash Conditional Statements Explained with Real Linux Examples
Bash Conditional Statements Explained with Real Linux Examples
All PowerShell Operators Explained
All PowerShell Operators Explained

Nested If-Else Statements

Shell scripts can contain nested if-else statements, allowing for more complex decision-making processes. Here's an example that checks if a number is within a specific range:

```bash read -p "Enter a number: " num if [ $num -ge 0 ] then if [ $num -le 10 ] then echo "The number is between 0 and 10." else echo "The number is greater than 10." fi else echo "The number is negative." fi ```

In this script, the outer if-else statement checks if the number is non-negative. If it is, the inner if-else statement checks if it's within the range of 0 to 10. If the number is negative, the script prints a message indicating that.

AD

Mastering if-else statements in shell scripts empowers you to create dynamic and versatile scripts tailored to your specific needs. Practice and experimentation are key to unlocking the full potential of this essential shell scripting construct.

Top 11 Resources to Learn Shell Scripting for Free
Top 11 Resources to Learn Shell Scripting for Free
The Complete How To Guide of Bash Functions
The Complete How To Guide of Bash Functions
Part 2: Stop Using If-Else Statements
Part 2: Stop Using If-Else Statements
Bash scripting cheatsheet
Bash scripting cheatsheet
echo: Display Text in Terminal
echo: Display Text in Terminal
How Bash Uses sed for Text Editing
How Bash Uses sed for Text Editing
Difference Between Bash and Dash | Compare Bash and Dash | Bash vs. Dash
Difference Between Bash and Dash | Compare Bash and Dash | Bash vs. Dash
How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
What Is Shell Scripting and Why You Should Use It
What Is Shell Scripting and Why You Should Use It
AWK Fundamentals for Beginners - Linux Text Processing Made Simple
AWK Fundamentals for Beginners - Linux Text Processing Made Simple
How to Create/Write a Simple/Sample Linux Shell/Bash Script
How to Create/Write a Simple/Sample Linux Shell/Bash Script
Bash Case Statement with Examples for Linux Beginners & Sysadmins
Bash Case Statement with Examples for Linux Beginners & Sysadmins
7 Bash exit codes every sysadmin must know in 2026 - Part 5 / 34
7 Bash exit codes every sysadmin must know in 2026 - Part 5 / 34
HTML Semantics: <i> vs <em> – When to Use Which? 🗣️
HTML Semantics: <i> vs <em> – When to Use Which? 🗣️

As you continue your shell scripting journey, remember to explore other control flow constructs, such as case statements and loops, to further expand your scripting capabilities. Happy coding!

AD