Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

control-flow-bash

Control flow in Bash

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Control flow in Bash: the syntax, patterns, and safety habits you need before advancing in Bash.

Coroutines replace callback hell on Android and in Ktor—structured concurrency is interview-critical.

You will apply Control flow in Bash in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons.

When you can explain the previous lesson's ideas without copying starter code.

Bash supports if, case, and loops (for, while). Tests use [[ ]] (Bash) or [ ] (POSIX test)—prefer [[ ]] in Bash scripts.

if and [[ ]]

score=85
if [[ $score -ge 90 ]]; then
  echo "A"
elif [[ $score -ge 80 ]]; then
  echo "B"
else
  echo "C or below"
fi

-ge means greater than or equal (numeric test inside [[ ]]).

for loops

for f in *.txt; do
  echo "file: $f"
done

for n in 1 2 3; do
  echo "n=$n"
done

for f in *.txt loops over matching files—glob order matters for reproducibility.

while loop

count=0
while [[ $count -lt 3 ]]; do
  echo "count=$count"
  count=$((count + 1))
done

Important interview questions and answers

  1. Q: if vs [[ ]]?
    A: [[ is a Bash keyword with safer parsing than the external [ command.
  2. Q: for file in *.log purpose?
    A: Runs the loop body once per matching filename.

Self-check

  1. Which test operator means greater or equal in [[ ]]?
  2. What keyword closes an if block?

Tip: Prefer [[ ]] in Bash scripts; use [ ] when you must stay POSIX-sh compatible.

Interview prep

[[ ]] advantage?

Bash keyword with safer parsing than external [ test.

fi purpose?

Closes an if/elif/else block.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • [ vs [[?
  • case when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump