Bash If Else Statement
Bash If Else : If-else statement is used for conditional branching of program (script) execution between two paths.
An expression is associated with the if statement. If the expression evaluates to true, statements of if block are executed. If the expression evaluates to false, statements of else block are executed.
In this tutorial, we will go through Syntax and usage of if-else statement with examples.
Bash If Else is kind of an extension to Bash If statement.
Syntax of Bash If Else
Syntax of If Else statement in Bash Shell Scripting is as given below :
</>
Copy
if <expression>; then
<commands>
else
<other_commands>
fi
where
| Code | Description |
|---|---|
| <expression> | Set of one or more conditions joined using conditional operators. |
| <commands> | Set of commands to be run when the <condition> is true. |
| <other_commands> | Set of commands to be run when the <condition> is false. |
Observe that if, then, else and fi are keywords. The semi-colon (;) after the conditional expression is must.
