If Statement

Learn how the if statement works in detail, along with one-liners and fragile code.

Conditional statements

We first learned of conditional statements the first time when we learned about the find utility. Then, we found out that Bash has its own logical operators AND (&&) and OR (||). Bash also has other options to make branches.

The operators if and case are used frequently when writing Bash scripts. These operators execute similar behavior. However, each of them fits better for some specific tasks.

If statement

Let’s imagine that we write a one-line command. Such a command is called a one-liner. We try to make it as compact as possible because a short command is faster to type. Also, shorter code gives us fewer chances of making a mistake when typing.

Now, let’s imagine that we write a script. The hard drive stores it. We call the script regularly and change it rarely. The code’s concision is not important in this case. Instead, we try to make the ...