teluzia.Cloud Engineer · Day 13
CHAPTER 02 · LINUX ADMINISTRATION AND SHELL SCRIPTING

Bash variables, loops, conditions and error handling

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Write a Bash script that transforms a few fixed inputs and stops predictably on invalid input. Variables, conditions and loops are useful only when quoting and exit-status handling are reliable. A missing file will be the deliberate fault, and the script should explain it rather than continuing with fabricated output. Bash assigns a variable with name=value, without spaces around the equals sign. Expansion such as "$name" substitutes its value while preserving it as one argument. Unquoted expansions can undergo word splitting and filename expansion. Prefer printf over echo for predictable formatted output. Environment variables are exported to child processes deliberately. Bash if evaluates a command’s exit status, where zero means success. Tests such as [[ -f "$path" ]] ask specific questions without reading the file. for iterates over supplied words; while repeats while a condition succeeds. Choose an explicit input set rather than accidentally looping over shell-expanded filenames.

Think of it this way

Variables are labeled boxes, conditions are gates, loops are repeat stations, and exit codes are the red or green completion light.

TOPIC 01 / 03Step by step

Variables and quoting

Bash assigns a variable with name=value, without spaces around the equals sign. Expansion such as "$name" substitutes its value while preserving it as one argument. Unquoted expansions can undergo word splitting and filename expansion. Prefer printf over echo for predictable formatted output. Environment variables are exported to child processes deliberately. name="Ada Lovelace"; printf "Hello, %s\n" "$name" preserves the two-word name as one argument rather than two separate words. Quoting makes scripts behave consistently for spaces and wildcard characters and prevents user-controlled values from accidentally becoming several paths or options during routine processing tasks. Writing name = value attempts to run a command named name; quote expansions and keep assignment syntax compact instead of treating Bash like Python.

WORKED EXAMPLE
name="Ada Lovelace"; printf "Hello, %s\n" "$name" preserves the two-word name as one argument rather than two separate words.
Why this matters

Quoting makes scripts behave consistently for spaces and wildcard characters and prevents user-controlled values from accidentally becoming several paths or options during routine processing tasks.

A common trap

Writing name = value attempts to run a command named name; quote expansions and keep assignment syntax compact instead of treating Bash like Python.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Variables and quoting

Bash assigns a variable with name=value, without spaces around the equals sign. Expansion such as "$name" substitutes its value while preserving it as one argument. Unquoted expansions can undergo word splitting and filename expansion. Prefer printf over echo for predictable formatted output. Environment variables are exported to child processes deliberately. name="Ada Lovelace"; printf "Hello, %s\n" "$name" preserves the two-word name as one argument rather than two separate words. Quoting makes scripts behave consistently for spaces and wildcard characters and prevents user-controlled values from accidentally becoming several paths or options during routine processing tasks. Writing name = value attempts to run a command named name; quote expansions and keep assignment syntax compact instead of treating Bash like Python.

WORKED EXAMPLE
name="Ada Lovelace"; printf "Hello, %s\n" "$name" preserves the two-word name as one argument rather than two separate words.

Why this matters

Quoting makes scripts behave consistently for spaces and wildcard characters and prevents user-controlled values from accidentally becoming several paths or options during routine processing tasks.

A common trap

Writing name = value attempts to run a command named name; quote expansions and keep assignment syntax compact instead of treating Bash like Python.

Conditions and loops

Bash if evaluates a command’s exit status, where zero means success. Tests such as [[ -f "$path" ]] ask specific questions without reading the file. for iterates over supplied words; while repeats while a condition succeeds. Choose an explicit input set rather than accidentally looping over shell-expanded filenames. for label in alpha beta gamma; do printf "%s\n" "$label"; done emits three lines; if [[ -f "$path" ]]; then handles file existence. Conditions and loops automate repeated operations while keeping decision criteria visible, which helps you explain why each input was accepted, skipped or rejected during execution. Treating exit status zero as false reverses shell logic; unlike many arithmetic examples, command success in Bash is represented by zero and failures are nonzero.

WORKED EXAMPLE
for label in alpha beta gamma; do printf "%s\n" "$label"; done emits three lines; if [[ -f "$path" ]]; then handles file existence.

Why this matters

Conditions and loops automate repeated operations while keeping decision criteria visible, which helps you explain why each input was accepted, skipped or rejected during execution.

A common trap

Treating exit status zero as false reverses shell logic; unlike many arithmetic examples, command success in Bash is represented by zero and failures are nonzero.

Error handling and status

A script should return nonzero when it cannot fulfill its contract. Capture $? immediately after the command being examined, or test the command directly in if. set -e has contextual exceptions and is not a complete error strategy; pipefail helps reveal failing pipeline stages. Write diagnostics to standard error. if [[ ! -f "$path" ]]; then printf "Missing input: %s\n" "$path" >&2; exit 2; fi makes failure explicit before processing any data. Explicit errors prevent downstream steps from treating incomplete output as success and provide a stable contract for callers, schedulers and continuous integration tools that invoke automation. Printing an error while returning status zero misleads callers; pair diagnostic messages with a deliberate nonzero status and avoid overwriting the status before inspecting it.

WORKED EXAMPLE
if [[ ! -f "$path" ]]; then printf "Missing input: %s\n" "$path" >&2; exit 2; fi makes failure explicit before processing any data.

Why this matters

Explicit errors prevent downstream steps from treating incomplete output as success and provide a stable contract for callers, schedulers and continuous integration tools that invoke automation.

A common trap

Printing an error while returning status zero misleads callers; pair diagnostic messages with a deliberate nonzero status and avoid overwriting the status before inspecting it.

KEEP THESE WITH YOU

Today’s takeaways

Quote data expansions.

Use explicit decision conditions.

Make failures visible in stderr and exit status.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed