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 - 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
-
-
-
-
-
(Locked)
Conditional statements with the "if" keyword4m 32s
-
(Locked)
Working with while and until loops3m 56s
-
(Locked)
Introducing "for" loops5m 55s
-
(Locked)
Selecting behavior using "case"2m 8s
-
(Locked)
Using functions7m 6s
-
(Locked)
Reading and writing text files3m 59s
-
(Locked)
Challenge: Build a script using control structures31s
-
(Locked)
Solution: Build a script using control structures1m 39s
-
(Locked)
-
-
-