Lab 01: Shell Scripting — Variables & Conditionals

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm ubuntu:22.04 bash


Overview

In this lab you'll build a solid foundation in Bash scripting: declaring variables, understanding quoting rules, performing arithmetic, and controlling flow with if/elif/else and case statements. Every example is Docker-verified so you see exactly what to expect.


Step 1: Declaring and Using Variables

Variables in Bash are untyped and assigned without spaces around =.

name="Alice"
greeting="Hello, $name!"
echo "$greeting"
echo "Single: $name"
echo 'No expand: $name'

📸 Verified Output:

Hello, Alice!
Single: Alice
No expand: $name

💡 Tip: Always double-quote variable expansions ("$var") to prevent word-splitting on values that contain spaces. Single quotes treat everything literally — no variable expansion occurs inside '...'.


Step 2: Variable Quoting Deep Dive

Quoting is one of the most common sources of bugs. Practice the three quoting styles:

📸 Verified Output:

💡 Tip: Use ${var} (braces) when the variable name is immediately followed by alphanumeric characters, e.g., ${prefix}file instead of $prefixfile.


Step 3: Arithmetic Operations

Bash supports integer arithmetic via $(( )).

📸 Verified Output:

💡 Tip: $(( )) does integer arithmetic only. For floating-point math use bc or awk: echo "scale=2; 10/3" | bc3.33.


Step 4: Test Operators — Files and Strings

The [ ] command (aka test) evaluates conditions. File and string tests:

📸 Verified Output:

💡 Tip: Use [[ ]] (double brackets) in Bash scripts for safer string tests — it handles empty variables and glob patterns without quoting surprises.


Step 5: Test Operators — Numeric Comparisons

Numeric comparisons use letter-based operators inside [ ]:

📸 Verified Output:

💡 Tip: Don't use < or > for numeric comparison inside [ ] — those redirect files! Use -lt, -gt, etc., or use (( x > y )) with double parentheses for arithmetic truth tests.


Step 6: if / elif / else

Grade a score using chained conditionals:

📸 Verified Output:

💡 Tip: Conditions are evaluated top-to-bottom; the first match wins. Structure your elif branches from most restrictive to least restrictive for predictable logic.


Step 7: case Statement

case is cleaner than chains of if when matching one variable against multiple patterns:

📸 Verified Output:

💡 Tip: The * catch-all pattern at the end of a case acts like the else clause — always include it to handle unexpected input gracefully.


Step 8: Capstone — User Input Validator Script

Combine everything into a practical validator script. This simulates what you'd write to validate configuration values before a deployment:

📸 Verified Output:

💡 Tip: The capstone pattern — validate inputs, report clearly, return meaningful exit codes — is the backbone of reliable automation scripts. Build this habit early.


Summary

Concept
Syntax
Example

Variable assignment

name="value"

user="alice"

Double-quote expand

"$var"

echo "$name"

No expansion

'$var'

echo '$name'

Arithmetic

$(( expr ))

$((x + y))

File exists

[ -f path ]

[ -f /etc/hosts ]

Dir exists

[ -d path ]

[ -d /tmp ]

String empty

[ -z "$s" ]

test for blank

String non-empty

[ -n "$s" ]

test for content

Numeric equal

[ x -eq y ]

[ $a -eq 0 ]

Numeric less

[ x -lt y ]

[ $count -lt 10 ]

Numeric greater

[ x -gt y ]

[ $size -gt 100 ]

If/elif/else

if [...]; then ... fi

grade classifier

Case statement

case "$v" in pat) ;; esac

environment selector

Last updated