Bash Scripting - If Statement
# Bash Scripting - if Statement
The `if` statement is a fundamental concept in Bash scripting. It allows a script to make decisions on what code to execute based on the conditions of the system or user input. `if` statements enable us to take actions based on the result of a condition, and are essential for writing complex Bash scripts.
The `if` statement is used to check a given condition and, if the condition is true, execute the commands associated with it. The syntax of the `if` statement is as follows:
```
if [ condition ]
then
command1
command2
...
fi
```
The condition can be any expression that evaluates to `true` or `false`, based on the comparison of any given values or variables. If the condition evaluates to `true`, the commands within the `then` block will be executed, and if it evaluates to `false`, the commands will not be executed. It is also possible to add an `else` statement to the if statement, which will execute the commands within the `else` block if the condition evaluates to `false`.
The `if` statement is a powerful tool that can be used to check for any given condition, and execute code accordingly. It is an essential part of Bash scripting, and can be used to create complex scripts that can take decisions based on user input.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
If Statement
The if statement is used to execute a command or series of commands if a condition is met.
Syntax:
```
if [ condition ]
then
command1
command2
...
else
command3
command4
...
fi
```
Example:
```
#!/bin/bash
# Check if the value of the $num is greater than 10
num=20
if [ $num -gt 10 ]
then
echo "The number is greater than 10"
else
echo "The number is not greater than 10"
fi
```
Output:
The number is greater than 10