More Than Just Variables
You already know you can store data in a variable (NAME="Alice") and access it with $NAME. But Bash offers a family of parameter expansion features that let you transform or validate a variable's value at the moment you use it, without calling external tools like sed or awk. These expansions use curly braces: ${variable}.
1. Default values (${VAR:-default})
Provide a fallback if the variable is unset or empty, without altering the variable itself. This is essential for writing scripts that work with optional configuration.
2. Assign a default (${VAR:=default})
Behaves the same as the fallback above, but also assigns the default value back into the variable if it was unset — so the next reference to it sees the default too, not just this one.
3. Error on unset (${VAR:?message})
Exits the script immediately with the given error message if a required variable is missing or empty. This is the right tool for mandatory configuration — a required credential or hostname that the script simply cannot proceed without.
4. String length (${#VAR})
Returns the number of characters in a variable's value — useful for validation, such as confirming a generated value isn't unexpectedly empty.