Master Bash Conditionals and Loops: Practical Examples and Syntax Guide
Learn how to use Bash's flow-control structures—including if, elif, else, case, for, and while loops—through clear explanations and practical code examples that demonstrate testing conditions, handling files, and automating tasks in shell scripts.
Conditional statements and loops, collectively called flow control, form the most basic part of any programming language. Bash's flow control is very similar to that of familiar languages, so it is quick to pick up.
Conditional Statements
For Bash conditionals, start by reading the "Bash Test" documentation. The core of Bash conditionals is the Test command.
if
Example:
x=5;
if [ $x = 5 ]; then
echo 'x equals 5.';
else
echo 'x does not equal 5';
fi
# Output: x equals 5.Abstracted syntax:
if commands; then
commands
[elif commands; then
commands...]
[else
commands]
fiA more complex example using file tests:
FILE=~/.zshrc # any path
if [ -e "$FILE" ]; then # -e unary operator
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ]; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ]; then
echo "$FILE is writable."
fi
if [ -x "$FILE" ]; then
echo "$FILE is executable/searchable."
fi
else
echo "$FILE does not exist"
fiIn Bash, the Test command is the core of conditional statements, used after if and elif.
case
The case statement is similar to the familiar switch construct but with different syntax.
case "$variable" in
"$condition1" )
command...
;;
"$condition2" )
command...
;;
esacVariables are often quoted, but not required.
Each Test line must end with ).
Each condition block must end with ;;.
The entire case block ends with esac ("case" spelled backwards).
Example:
x=4
case $x in
'a' )
echo "x 是 a";;
4 )
echo "x 是 4";;
'b' )
echo "x 是 b"
esac
# x 是 4In summary, Bash conditionals rely heavily on the Test command, and mastering the syntax of if and case is sufficient for most tasks.
Loops
Bash provides two common loop constructs: for and while, both of which are familiar to programmers.
for
Example: batch rename files.
Directory layout:
.
├── error_400.html
├── error_403.html
├── error_404.html
├── error_500.html
└── error_503.htmlBash code:
for $i in `ls`
do
mv $i ${i/html/ejs};
doneGeneral syntax:
for variable [in words]; do
commands
done docan appear on a new line or on the same line as for (in which case for must end with ;).
The loop body must end with done. [in words] can be a wildcard, a command such as ls, or any list; it must be provided in array form.
Another simple example:
for i in *
do
echo $i;
done
## prints all filenames in the current directorywhile
Example:
count=1
while [ $count -le 5 ]; do
echo $count
count=$((count + 1))
done
echo "Finished."
# prints 1 - 5 and Finished.Syntax:
while commands; do commands; doneReference Articles
http://wiki.jikexueyuan.com/project/linux-command/chap28.html
http://tldp.org/LDP/abs/html/testbranch.html
http://wiki.jikexueyuan.com/project/linux-command/chap30.html
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.
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
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.
