# Shell shell-basics ## Variables **Variables** store values. Assign with `VAR=value` (no spaces around `=`). Access with `$VAR` or `${VAR}`. Variables are case-sensitive and untyped—everything is a string unless used in arithmetic. ```bash $ NAME="Alice" $ echo $NAME # prints: Alice $ echo ${NAME}_suffix # prints: Alice_suffix $ X=5; Y=$((X + 3)); echo $Y # arithmetic: prints 8 ``` Unset variables are empty strings. Quote variable references to preserve spaces: `"$VAR"` vs `$VAR`. ## Quoting and escaping **Quoting** controls how the shell interprets special characters. Double quotes `"..."` preserve most meaning but allow expansion of `$VAR` and backticks. Single quotes `'...'` prevent all expansion. Backslash `\` escapes a single character. ```bash $ echo "Hello $USER" # expands $USER $ echo 'Hello $USER' # literal: Hello $USER $ echo "Cost: \$5" # literal: Cost: $5 $ echo $'Hello\nWorld' # ANSI-C quoting: interprets \n ``` Use single quotes for literals, double quotes when you need expansion. ## Expansion **Expansion** is how the shell transforms text before running commands. Types include: - **Parameter expansion**: `$VAR`, `${VAR}`, `${VAR:-default}` - **Arithmetic expansion**: `$((2 + 2))` - **Command substitution**: `$(command)` or `` `command` `` - **Brace expansion**: `{a,b,c}` expands to `a b c` - **Tilde expansion**: `~` becomes `$HOME` - **Pathname expansion (globbing)**: `*.txt` matches files ending in `.txt` ```bash $ echo {1,2,3} # expands to: 1 2 3 $ echo /home/*/Desktop # expands to matching directories $ echo "Today is $(date +%Y)" # command substitution ``` Expansions happen after quoting, so `"$VAR"` expands but `'$VAR'` does not.