Shell FundamentalsLesson 1.2
Bash variables and quoting rules explained
variable assignment, dollar-sign expansion, single vs double quotes, unset variables, readonly, local scope, word splitting
Variables in Bash
Bash variables have no types — everything is a string. Assignment uses = with no spaces around it (spaces break parsing).
name="Alice"
count=42
path_to_logs=/var/log/app
echo $name # Alice
echo "Hello $name" # Hello Alice
echo 'Hello $name' # Hello $name ← literal, no expansionQuoting Rules — Get These Wrong, Break Everything
Double quotes "...": allow variable and command expansion. Use for almost everything.
Single quotes '...': treat everything literally. No expansion at all.
No quotes: enables word splitting and glob expansion — dangerous with filenames containing spaces.
file="my report.txt"
rm $file # WRONG: tries to rm 'my' and 'report.txt' separately
rm "$file" # CORRECT: treats as one argumentSpecial Variables
$0 # Script name
$1, $2 ... $9 # Positional arguments
$# # Number of arguments
$@ # All arguments (as separate words)
$? # Exit code of last command
$$ # PID of current shellAlways quote "$@" when passing arguments forward — it preserves argument boundaries even if arguments contain spaces. This single habit prevents an entire class of script bugs.
