From the course: Learning Bash Scripting

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Conditional statements with the "if" keyword

Conditional statements with the "if" keyword - Bash Tutorial

From the course: Learning Bash Scripting

Conditional statements with the "if" keyword

When we write scripts, we'll often use control structures. Instead of just running script commands from the top down, one after another, control structures give us the opportunity to control and change how script commands are executed based on conditions we specify. One of these control structures is the if statement, and an if statement runs or executes code based on the truth value of a given expression. In Bash, that takes the form of if followed by an expression from which we'll get a true or false value, followed with a keyword "then." That's followed by the script lines to run if the condition evaluates as true. We can end the if statement there with fi, the word "if" spelled backwards, or we can add an else statement and provide some code to run if the condition evaluates as false. The condition is something that returns a truth value, often the extended test with two brackets, but it can also be the older single-bracket test or an arithmetic evaluation. It can also be a…

Contents