100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Bash

Review of Bash Basics

A refresher on core Bash concepts—shell startup, variables, quoting, and control flow—that underpin the advanced scripting techniques covered later in this course.

Advanced FundamentalsBeginner8 min readJul 10, 2026
Analogies

Why Revisit the Basics

Before diving into advanced techniques like parameter expansion, arrays, and associative arrays, it helps to firm up the fundamentals: how Bash starts up, how variables are scoped, how quoting prevents unwanted word splitting, and how control-flow constructs like if, for, while, and case actually evaluate their conditions. Advanced scripting is basic scripting applied with more discipline and fewer assumptions.

🏏

Cricket analogy: A batsman does not skip throwdowns and slip-catching drills before facing a Bumrah yorker in a World Cup final; mastering advanced Bash means first re-drilling variable assignment and quoting before touching trap handlers and associative arrays.

Shells, Startup Files, and Variables

When Bash starts, it reads different startup files depending on whether it is an interactive login shell, an interactive non-login shell, or a non-interactive shell running a script; login shells typically source /etc/profile and ~/.bash_profile, while interactive non-login shells source ~/.bashrc, and scripts inherit only exported environment variables. Variables in Bash are untyped strings by default, created with NAME=value with no spaces around the equals sign, and become available to child processes only after being exported with the export command.

🏏

Cricket analogy: A player's kit changes depending on the format—Test whites and a red ball for a five-day match versus coloured clothing and a white ball for a T20—just as Bash's behaviour changes depending on whether it is a login, interactive, or script shell.

bash
#!/bin/bash
# Variable assignment (no spaces around =)
name="SkillVeris"
count=42

# Exporting makes it visible to child processes
export GREETING="Hello, $name!"

# Quoting matters: unquoted expansion undergoes word splitting
files=*.txt
echo $files          # word-split, glob-expanded
echo "$files"         # literal string "*.txt"

# Command substitution
today=$(date +%Y-%m-%d)
echo "Report generated on $today"

Quoting and Word Splitting

Unquoted variable expansions are subject to word splitting on $IFS and filename generation (globbing), which is why a variable holding a filename with spaces can silently break a script; wrapping expansions in double quotes, as in "$var", preserves the value as a single word while still allowing variable and command substitution to occur, whereas single quotes suppress all expansion entirely.

🏏

Cricket analogy: An unquoted variable is like an unguarded single stump—one gust of wind (word splitting) and the bails fall; double-quoting a variable is like securing all three stumps so the delivery only affects what it should.

Never leave variable expansions unquoted when the value might contain spaces, globs, or come from user input—unquoted expansion is one of the most common sources of subtle bugs and security issues in shell scripts. As a habit, quote every expansion unless you specifically need word splitting or globbing to occur.

Control Flow: if, for, while, and case

Bash's if statement tests the exit status of a command (0 means true), commonly paired with the [[ ]] extended test command for string and file comparisons; for loops iterate over a list of words or a C-style numeric range; while loops repeat as long as a test succeeds; and case provides pattern-matching branching that is often clearer than a long if/elif chain when matching against multiple string patterns.

🏏

Cricket analogy: An umpire's decision review system checks a single condition—out or not out—just like Bash's if checks a command's exit status; a case statement is closer to the third umpire checking multiple possible dismissal types (caught, lbw, run out) at once.

bash
# if / elif / else with [[ ]]
if [[ -f "$1" ]]; then
  echo "File exists: $1"
elif [[ -d "$1" ]]; then
  echo "Directory exists: $1"
else
  echo "Not found: $1"
fi

# for loop over a list
for lang in bash python go; do
  echo "Reviewing basics of $lang"
done

# while loop
count=0
while (( count < 3 )); do
  echo "Iteration $count"
  ((count++))
done

# case statement
case "$1" in
  start) echo "Starting service" ;;
  stop)  echo "Stopping service" ;;
  *)     echo "Usage: $0 {start|stop}" ;;
esac
  • Bash reads different startup files (/etc/profile, ~/.bash_profile, ~/.bashrc) depending on whether the shell is login, interactive non-login, or a script.
  • Variables are untyped strings assigned with NAME=value (no spaces) and only visible to child processes once exported.
  • Unquoted variable expansions undergo word splitting on $IFS and filename globbing; always quote expansions unless splitting is intentional.
  • Double quotes allow variable and command substitution while preserving a single word; single quotes suppress all expansion.
  • if tests a command's exit status (0 = true), often via the [[ ]] extended test command for strings and files.
  • for iterates over a word list or numeric range; while repeats until a test fails; case matches string patterns cleanly.
  • These basics are the foundation for parameter expansion, arrays, and associative arrays covered next in this course.

Practice what you learned

Was this page helpful?

Topics covered

#Bash#AdvancedBashScriptingStudyNotes#DevOps#ReviewOfBashBasics#Review#Revisit#Shells#Startup#StudyNotes#SkillVeris