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.
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
fiCondition 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!"
fiShell Loop Statements (for, while, until)
for loop iterates over a list of values.
for var in list; do
action
doneExample using seq to generate numbers 1‑10:
#!/bin/sh
for i in $(seq 10); do
echo $i
doneAnother form uses C‑style syntax:
#!/bin/sh
for ((i=1;i<=10;i++)); do
echo $i
donewhile loop repeats while a condition is true.
#!/bin/sh
i=10
while [[ $i -gt 5 ]]; do
echo $i
((i--))
doneReading a file line‑by‑line:
#!/bin/sh
while read line; do
echo $line
done < /etc/hostsuntil loop runs until its condition becomes true.
#!/bin/sh
a=10
until [[ $a -lt 0 ]]; do
echo $a
((a--))
doneShell Selection Statements (case, select)
The case statement matches a variable against patterns.
case $arg in
pattern|sample) # action ;;
pattern1) # action ;;
*) # default action ;;
esacExample:
#!/bin/sh
case $1 in
start|begin) echo "start something" ;;
stop|end) echo "stop something" ;;
*) echo "Ignorant" ;;
esacThe 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
doneThese constructs form the core of shell flow‑control, enabling conditional execution, iteration, and menu‑driven selection in scripts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
