Skip to main content

Learn Bash Scripting

Chapter #9: How to Use Until Loops and Case Statements in Bash

In this chapter, we learn how to use until loops and case statements in Bash to make our scripts smarter and more flexible.

Loops are a powerful tool in Bash scripting that help make our scripts more efficient and dynamic. Instead of repeating the same block of code over and over, loops allow us to automate and make our code shorter, cleaner, and easier to maintain.

In the previous chapters, we learned how to use the for and while loops to repeat tasks. These loops are great for working with data, especially when dealing with files, directories, and other elements in a system.

But did you know there are other types of loops in Bash? In this chapter, we’ll explore a couple of them: the until loop and the case statement. These are additional ways to make our scripts more flexible and powerful.

Let’s dive into these new loops and see how they work with examples!

Until Loop

The until loop works similarly to the while loop, but it runs in the opposite way.

In a while loop, the code runs as long as the condition is true. But in an until loop, the code runs until the condition becomes true. Once the condition is true, the loop stops.

The basic syntax of an until loop looks like this:

until [ condition ]
do
    command to execute
done

Just like the while loop, we place the condition inside the brackets. The until loop checks the condition and keeps running the code inside the loop until the condition becomes true. Once true, the loop exits.

Let’s take a look at a simple example:

a=1

until [ $a -gt 6 ]
do
    echo "$a"
    ((a++))
done

Here’s what happens:

  1. We declare a variable a and set it to 1.
  2. The until loop runs as long as a is not greater than 6.
  3. Inside the loop, we print the value of a and then increase it by 1.
  4. The loop keeps running until a is greater than 6.

In this case, the loop will print the numbers 1 through 6. Once a becomes 7, the condition [ $a -gt 6 ] becomes true, and the loop stops.

Until Loop: A Simple Countdown in Bash

The until loop is the opposite of the while loop. It keeps running until the condition becomes true, whereas the while loop runs as long as the condition is true.

When to Use an Until Loop

The until loop isn’t as commonly used as the while loop, but there are cases where it’s helpful.

For example, let’s say you’ve seen scripts that keep asking you to enter a password until you get it right. This is a great use case for the until loop.

Here’s how we could use an until loop to create a password prompt:

password="tecmint"
input=""

until [ "$input" == "$password" ]; do
    read -sp "Enter password: " input
    echo  
    if [ "$input" != "$password" ]; then
        echo "Incorrect password. Try again."
    fi
done

echo "Welcome! Pro.TecMint"

Let’s break this down:

  1. We set the password to "tecmint" and leave the input variable is empty for now.
  2. The loop keeps asking for the password until the input matches the password.
  3. The read command takes user input, and the -s flag hides the input (it appears as asterisks).
  4. If the entered password is wrong, the message "Incorrect password. Try again." is shown.
  5. Once the correct password is entered, the script prints "Welcome! Pro. TecMint."

When you run this script, it will keep asking you for the password until you type it correctly. Once you do, it will say "Welcome! Pro. TecMint."

Use the Until Loop for Password Validation in Bash

Case Statement

The case statement is kind of like an if statement, but with a twist. Instead of checking one condition and doing one thing, the case statement lets you check one condition and choose between multiple options.

If you've used the switch statement in other languages like C, this will feel familiar, but with some Bash flavor added in.

Here’s the basic syntax:

case test_expression in
    choice1)
        # commands to run
        ;;
    choice2)
        # commands to run
        ;;
    choice3)
        # commands to run
        ;;
esac

Let’s break this down:

  • We start with the case keyword followed by a value or variable we want to test.
  • Then we use the keyword in to start listing the possible matches.
  • Each option is followed by a closing ) and ends with ;;.
  • Finally, we wrap it all up with esac (that’s “case” spelled backwards).

Let’s break it down with a simple example:

#!/bin/bash

echo "Enter your favorite website"
read name

case $name in
    "tecmint")
        echo "You entered TecMint."
        ;;
    "linux")
        echo "You entered Linux."
        ;;
    "youtube")
        echo "You entered YouTube."
        ;;
    *)
        echo "Invalid name."
        ;;
esac

Here’s what’s going on:

  1. We ask the user to type their favorite website.
  2. We store that input in a variable called name.
  3. The case statement checks the value of $name against a few known choices:
    • If it’s "tecmint", it prints a message.
    • Same for "linux" and "youtube".
    • The * is a wildcard, it catches anything that doesn't match the choices above and prints “Invalid name.”
  4. Each block ends with ;; to separate the choices.
💡
Quick tip: The case statement in Bash is case-sensitive, so "tecmint" is not the same as "Tecmint".

If we run this, we should get this output:​

Use Case Statement in Bash

You should note that the case statement is case-sensitive; the word "tecmint" is not equal to "Tecmint."

Summary

Now you’ve added two more powerful tools to your Bash scripting toolbox: the until loop and the case statement.

The until loop helps you run code until a condition becomes true - perfect for things like password prompts or countdowns. It’s the opposite of a while loop but just as useful when the situation calls for it.

The case statement, on the other hand, gives you a cleaner way to handle multiple choices. It’s great for matching user input or checking different values without writing a bunch of if statements.

Up next, we’ll look at even more real-world examples and practical scripts you can build using everything you’ve learned so far.