Scripting & Automation

Variables, Loops & Conditionals

Control flow and data handling in bash.

35 min read beginner 3 objectives

Status

Not started

What you will learn

  • Use conditionals and tests
  • Iterate with loops
  • Work with arrays

New to this? Start here

The basics, in plain English

To do anything useful, a script needs to remember values and make decisions. Variables store values, loops repeat actions, and conditionals choose between paths. These three ideas exist in every programming language.

Variable
A named box that holds a value, like name="Alice", so you can reuse it later.
Loop
A way to repeat the same steps many times, such as once for every file.
Conditional (if)
A check that runs one block of code only when something is true.
Comparison
Testing whether two things are equal, bigger, smaller, or a file exists.
Array
A variable that holds a list of values instead of just one.
01

Control flow

if/elif/else with [[ ]] tests, for and while loops, and case statements cover almost everything you need in ops scripts.

Try it yourself

bash
$for f in *.log; do
Loop over every .log file in the folder.
$ [[ -s "$f" ]] && gzip "$f"
If the file is non-empty, compress it.
$done
End of the loop.
↳ lines explain what each command does — only the commands get copied

Finished this topic?

Mark it done to earn 100 XP and keep your streak alive.