name='Jonathan'Do not put spaces around = in shell assignment.
Bash variables, parameter expansion, quoting rules, command substitution, arithmetic, and shell expansion patterns.
Assign, export, inspect, and unset variables.
name='Jonathan'Do not put spaces around = in shell assignment.
echo "$name"Double quotes preserve spaces and most characters.
export APP_ENV=productionNeeded when subprocesses should receive the value.
unset nameUseful for cleanup and testing default expansion behavior.
Use a fallback if variable is unset or empty.
echo "${PORT:-3000}"One of the most common parameter expansion patterns.
Assign default if variable is unset or empty.
echo "${PORT:=3000}"Be careful because this mutates the variable.
Abort expansion with error if variable is missing.
echo "${DATABASE_URL:?DATABASE_URL is required}"Great for defensive scripts and deployment checks.
Use alternate text when variable is set and non-empty.
echo "${DEBUG:+enabled}"Good for optional flags and output labels.
Single quotes, double quotes, escaping, and ANSI-C quoting.
echo 'Path is $HOME'Use when you need literal shell characters.
Double quotes allow variable and command expansion.
echo "Path is $HOME"Usually the safest choice around variable expansions.
echo "A quote: \""Needed when embedding quotes in strings.
printf '%s
' $'line1
line2'Bash supports $'...' expansion for escaped literals.
echo '\$HOME'Handy when writing examples or templates.
Substring, replacement, command substitution, and arithmetic.
today=$(date +%F)Prefer $(...) over backticks for clarity and nesting.
echo $((2 + 3 * 4))Useful for counters, lengths, and simple math.
echo "${#name}"Works for strings and arrays.
echo "${name:0:4}"Offsets are zero-based.
echo "${file/.txt/.bak}"Useful for filename transforms.
echo "${path//\//:}"The doubled slash means global replacement.
echo "${path#*/}"Handy for path processing.
echo "${file%%.*}"Useful for basename-like operations and extensions.
Filename matching and brace generation.
printf '%s
' *.txtGlobs are expanded by the shell before command execution.
printf '%s
' file-{a,b,c}.txtBrace expansion happens before pathname expansion.
printf '%s
' {1..5}Great for loops and quick test data.
shopt -s nullglobSafer than leaving a literal pattern in loops.
shopt -s dotglobUseful for file moves and maintenance tasks.