Operations 6 min read

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.

Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Master Bash Conditionals and Loops: Practical Examples and Syntax Guide

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:

<code>x=5;

if [ $x = 5 ]; then
    echo 'x equals 5.';
else
    echo 'x does not equal 5';
fi

# Output: x equals 5.
</code>

Abstracted syntax:

<code>if commands; then
    commands
[elif commands; then
    commands...]
[else
    commands]
fi
</code>

A more complex example using file tests:

<code>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"
fi
</code>

In 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.

<code>case "$variable" in
    "$condition1" )
        command...
    ;;
    "$condition2" )
        command...
    ;;
esac
</code>

Variables 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:

<code>x=4

case $x in
    'a' )
        echo "x 是 a";;
    4 )
        echo "x 是 4";;
    'b' )
        echo "x 是 b"
esac

# x 是 4
</code>

In 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:

<code>.
├── error_400.html
├── error_403.html
├── error_404.html
├── error_500.html
└── error_503.html
</code>

Bash code:

<code>for $i in `ls`
do
    mv $i ${i/html/ejs};
 done
</code>

General syntax:

<code>for variable [in words]; do
    commands
 done
</code>
do

can 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:

<code>for i in *
 do
    echo $i;
 done

## prints all filenames in the current directory
</code>

while

Example:

<code>count=1
while [ $count -le 5 ]; do
    echo $count
    count=$((count + 1))
 done
 echo "Finished."

# prints 1 - 5 and Finished.
</code>

Syntax:

<code>while commands; do commands; done
</code>

Reference 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

automationbashshell scriptingLoopsconditionals
Tencent IMWeb Frontend Team
Written by

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.

0 followers
Reader feedback

How this landed with the community

login 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.