Skip to main content

Command Palette

Search for a command to run...

case and loop in Bash

Published
3 min readView as Markdown
case and loop in Bash
X

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.

"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."

Here's the general syntax of the case statement:

case value in
    pattern1)
        # code to execute for pattern1
        ;;
    pattern2)
        # code to execute for pattern2
        ;;
    pattern3 | pattern4)
        # code to execute for pattern3 or pattern4
        ;;
    *)
        # code to execute for all other cases
        ;;
esac

In this syntax, value represents the value or variable being tested, and in is used to separate value from the list of patterns or values to match against.

Each pattern is followed by a single bracket ), and the code to execute for that specific pattern is enclosed between ) and ;;. The double semicolon ;; marks the end of each case.

The *) is a wildcard pattern that matches any value that doesn't match the previous cases. It is typically used as the last case and represents the default case when no other pattern matches.

In Bash scripting, the for loop can be used in two different forms:

  1. for var in list: This is the basic form of the for loop, where var is a variable that takes each value from the list in each iteration of the loop. The list can be a space-separated sequence of items, a wildcard expression, or the result of a command substitution.

  2. for (( expr1 ; expr2 ; expr3 )): This form of the for loop, known as the "C-style" for loop, allows you to use arithmetic expressions and specify the initialization, condition, and update expressions. The loop variable is usually defined as a numerical variable using (( )).

Here's an example to illustrate the difference between the two forms:

# Basic form of the for loop
for fruit in apple banana cherry
do
    echo "I like $fruit"
done

# C-style for loop
for (( i = 1; i <= 5; i++ ))
do
    echo "Count: $i"
done

In the first example, the basic form of the for loop is used to iterate over a list of fruits. In each iteration, the loop variable fruit takes the value of each item in the list (apple, banana, cherry), and the corresponding code block is executed.

In the second example, the C-style for loop is used to iterate from 1 to 5. The loop variable i is initialized to 1, the loop continues as long as i is less than or equal to 5, and i is incremented by 1 in each iteration. The code block inside the loop is executed five times, displaying the current count.

So, to answer your question, you use the basic form of the for loop without the double parentheses ((( ))) when you want to iterate over a list of items or use wildcard expressions. On the other hand, you use the C-style for loop with double parentheses when you need to perform arithmetic operations and control the loop behavior with explicit initialization, condition, and update expressions.

More from this blog

PERSONAL BLOG

110 posts

Use the search button to search for a specific topic or keyword *all posts are updated on the go*