Python Bash Variables: Declaration, Scope & Parameter Expansion
Variables in Bash store strings and numbers. Variable assignment strictly forbids spaces around the `=` sign. Parameter expansion (`${VAR}`) unlocks powerful string transformations, default value fallbacks, length inspection, and substring extraction directly without spawning external processes.
"A variable is a labeled container. Parameter expansion (`${VAR}`) is a smart lid that can automatically supply a backup item if the container is empty, or trim unwanted prefixes."
Deep Dive: How It Works
No Spaces Around `=`: `NAME="Alex"` (valid) vs `NAME = "Alex"` (error: bash tries to execute command NAME with argument =).
Quotes Matter: Double quotes (`"$VAR"`) expand variables and preserve whitespace; Single quotes (`'$VAR'`) treat all characters literally.
Parameter Expansion: `${VAR:-default}` (use default if unset), `${#VAR}` (string length), `${VAR//old/new}` (search and replace), `${VAR:offset:length}` (substring).
Syntax Blueprint
NAME="ZenCompiler"
echo "Length: ${#NAME}"
PORT="${PORT:-8080}" # Fallback to 8080
UPPER="${NAME^^}" # Uppercase expansion
LOWER="${NAME,,}" # Lowercase expansionUse ${VAR:-default} for safe fallback configuration in scripts.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Writing spaces around `=`: `PORT = 8080`.Why it happens: Common habit from languages like Python or JavaScript.
How to fix: Never put spaces around the assignment operator in Bash: `PORT=8080`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Use Default Parameter Expansion
Set `CONFIG_ENV=""`.
Assign `ACTIVE_ENV="${CONFIG_ENV:-production}"`.
Echo "Active Environment: $ACTIVE_ENV".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.