Operations 6 min read

Master Linux Shell Flow Control: If, For, While, Until, and Case Explained

This article provides a comprehensive guide to Linux shell flow‑control constructs, covering conditional statements (if/elif/else), loops (for, while, until), and selection statements (case, select), complete with syntax explanations, practical examples, and execution results to help readers master shell scripting.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Shell Flow Control: If, For, While, Until, and Case Explained

Shell Conditional Statements (if)

Linux shell provides conditional statements using the if, then, elif, else, and fi keywords.

if [ condition ]; then
    action
elif [ condition ]; then
    action
else
    action
fi

Condition tests can use logical operators such as [[ and ]]. Example:

#!/bin/sh
scores=40
if [[ $scores -gt 90 ]]; then
    echo "very good!"
elif [[ $scores -gt 80 ]]; then
    echo "good!"
elif [[ $scores -gt 60 ]]; then
    echo "pass!"
else
    echo "no pass!"
fi

Shell Loop Statements (for, while, until)

for loop iterates over a list of values.

for var in list; do
    action
done

Example using seq to generate numbers 1‑10:

#!/bin/sh
for i in $(seq 10); do
    echo $i
done

Another form uses C‑style syntax:

#!/bin/sh
for ((i=1;i<=10;i++)); do
    echo $i
done

while loop repeats while a condition is true.

#!/bin/sh
i=10
while [[ $i -gt 5 ]]; do
    echo $i
    ((i--))
 done

Reading a file line‑by‑line:

#!/bin/sh
while read line; do
    echo $line
done < /etc/hosts

until loop runs until its condition becomes true.

#!/bin/sh
a=10
until [[ $a -lt 0 ]]; do
    echo $a
    ((a--))
done

Shell Selection Statements (case, select)

The case statement matches a variable against patterns.

case $arg in
    pattern|sample) # action ;;
    pattern1) # action ;;
    *) # default action ;;
esac

Example:

#!/bin/sh
case $1 in
    start|begin) echo "start something" ;;
    stop|end)   echo "stop something"  ;;
    *)         echo "Ignorant"       ;;
esac

The select statement creates a simple menu for user choice, often used together with case.

#!/bin/sh
select ch in "begin" "end" "exit"; do
    case $ch in
        "begin") echo "start something" ;;
        "end")   echo "stop something"   ;;
        "exit")  echo "exit"; break      ;;
        *)        echo "Ignorant"        ;;
    esac
done

These constructs form the core of shell flow‑control, enabling conditional execution, iteration, and menu‑driven selection in scripts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Flow Controlfor loopwhile-loopif statementCase statement
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.