Control Flow

8. Control Flow#

This chapter turns expressions into script structure: branches, cases, loops, loop control, interactive input, and command-line arguments.

  • Branching: Use if, elif, else, and case to choose among actions.

  • Loops: Repeat commands with for, while, and until.

  • Loop control: Use break and continue to leave or skip loop iterations.

  • Input: Read values from users and scripts without losing quoting safety.

  • Arguments: Use positional parameters to make scripts reusable.

Chapter Video

How To Write Bash Scripts In Linux - Complete Guide (Part 9 - For Loops) (12:32) by Learn Linux TV

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Write if, elif, else, and case branches.

  2. Use for, while, and until loops for repetition.

  3. Control loops with break and continue.

  4. Read user input and process script arguments.

Chapter Flow

Glossary

#

Term

Explanation

1

Control flow

The order in which a script chooses and repeats commands.

2

Branch

A path of execution selected by a condition.

3

if statement

A conditional structure that runs commands when a test succeeds.

4

elif

An additional conditional branch checked after an earlier if fails.

5

else

The fallback branch when no prior condition succeeds.

6

case statement

A branching structure that matches a value against patterns.

7

Pattern

A shell matching expression used by case or globbing.

8

for loop

A loop that iterates over a list of words or values.

9

while loop

A loop that repeats while a command or test succeeds.

10

until loop

A loop that repeats until a command or test succeeds.

11

Loop variable

A variable assigned a new value during each loop iteration.

12

break

A command that exits the current loop.

13

continue

A command that skips to the next loop iteration.

14

Nested loop

A loop placed inside another loop.

15

read

A Bash builtin that reads input into variables.

16

Prompt

Text shown to request user input.

17

Positional argument

A command-line argument available as $1, $2, and so on.

18

shift

A command that moves positional parameters left.

19

Input validation

Checking user or file input before using it.

20

Script interface

The arguments, prompts, and outputs through which users interact with a script.