Thuta Learning
IntermediateDevOps & Toolsbeginner

Shell Scripting: if, loops, functions

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand if, loops, and functions in shell scripting without any of the intimidation
  • Get hands-on practice trying out the commands yourself in the terminal
  • See how these commands actually come in handy on a real server or project

Let's think about it this way for a second

An if statement makes a decision based on a condition — written in the form if [ condition ]; then ... fi, and it's a strict syntax rule that you leave a space on both sides inside the [ ] (skip the space and you'll get an error). A for loop runs a command repeatedly for each item in a list — handy when you want to perform an action for every file in a folder, for instance. And a function is a way of naming a group of commands so you can reuse them — you create one with function_name() { commands } and just calling function_name runs every command inside it.

Let's connect this to a real-world scenario

In a backup script, it's common to first check whether a folder exists with if [ -d "$FOLDER" ] and only proceed with the backup if it does (-d checks for a directory, -f checks for a file). Something like for file in *.txt; do echo "Processing $file"; done will print a message for every .txt file in the folder — in real-world scripts, you can swap that echo out for backup, compress, or upload commands. And once you've defined functions, you no longer need to duplicate the same code over and over in longer scripts.

Let's try it together in the terminal

bash
#!/bin/bash
FOLDER="backups"

if [ -d "$FOLDER" ]; then
  echo "$FOLDER exists"
else
  mkdir "$FOLDER"
  echo "Created $FOLDER"
fi

for file in *.txt; do
  echo "Found: $file"
done

greet() {
  echo "Hello, $1!"
}
greet "Linux"
You should see
It shows 'Created backups' (if the folder doesn't exist yet) or 'backups exists', then lists out the .txt files, and finishes with 'Hello, Linux!'

5-minute try-it

Write a script called check-file.sh — have it accept an argument (a filename) and use an if statement to say whether the file exists.

A quick word of caution

If you put an rm command inside a for loop, running the script can automatically delete a whole bunch of files in one go — before running a loop that contains a dangerous command, do a dry run with echo first.

Easy traps

  • Getting a syntax error from writing if [$FOLDER] without spaces around the brackets — it should be if [ $FOLDER ], with spaces
  • Forgetting to quote a variable ("$FOLDER"), which causes errors with folder names that contain spaces

Now try it yourself

Write a script called check-file.sh — have it accept an argument (a filename) and use an if statement to say whether the file exists.

You'll know it worked when: It shows 'Created backups' (if the folder doesn't exist yet) or 'backups exists', then lists out the .txt files, and finishes with 'Hello, Linux!'