Control Flow

Overview

Control flow statements in Bash scripting allow you to control the execution of your script based on conditions and loops.

Code

If Statement

The if statement tests a condition and executes a block of code if the condition is true.

if [ condition ]; then
    # code to execute if condition is true
fi

If-Else Statement

The if-else statement executes different code blocks based on whether the condition is true or false.

if [ condition ]; then
    # code to execute if condition is true
else
    # code to execute if condition is false
fi

Elif Statement

The elif (else if) statement allows you to test multiple conditions.

if [ condition1 ]; then
    # code to execute if condition1 is true
elif [ condition2 ]; then
    # code to execute if condition2 is true
else
    # code to execute if both conditions are false
fi

For Loop

The for loop repeats a block of code for each item in a list.

for variable in list; do
    # code to execute for each item in the list
done

While Loop

The while loop repeats a block of code as long as a condition is true.

while [ condition ]; do
    # code to execute while condition is true
done

Case Statement

The case statement matches a variable against patterns and executes different code for each match.

case variable in
pattern1)
    # code to execute if variable matches pattern1
    ;;
pattern2)
    # code to execute if variable matches pattern2
    ;;
*)
    # default case
    ;;
esac

Details

The conditions in these control flow statements are tested using test constructs:

  • [ condition ] - Traditional test syntax (POSIX compliant)
  • [[ condition ]] - Modern Bash syntax with more features (regex, pattern matching)

The double bracket [[ ]] construct is preferred in Bash as it supports more complex conditions and regular expressions.


Appendix

Note created on 2025-12-23 and last modified on 2025-12-23.

See Also


(c) No Clocks, LLC | 2025