variables
unset
Removes a variable or function from the shell — different from setting it to empty. Without a flag it targets variables first, then functions, unless -f or -v is specified.
unset [-fv] NAME ... Common flags / operators
| Flag / Operator | Purpose |
|---|---|
| -v | Only unset variables (default preference) |
| -f | Only unset functions |
| array[i] | Remove a single array element |
| readonly | Cannot unset a readonly variable — it errors |
Examples
unset FOO Removes variable FOO entirely (differs from FOO='').
unset -f myfn Remove function myfn from the shell.
unset 'arr[2]' Delete element at index 2 (indices are not renumbered).
unset arr Delete the whole array.
Gotcha
Under 'set -u', referencing an unset variable is an error — 'unset FOO' followed by echo "$FOO" then aborts. Quote array element removal ('arr[2]') to prevent glob expansion.
Related
export
Marks a shell variable (or function with -f) for export to the environment of subsequently executed commands. Without arguments, prints all exported variables in a re-importable format.
declare / typeset / readonly
Declares variables with attributes: arrays, associative arrays, integers, readonly, lowercase/uppercase, exported. 'readonly' is a shortcut for 'declare -r' that locks a variable so it cannot be reassigned or unset for the lifetime of the shell.
set (options)
Changes shell option flags and/or replaces positional parameters. The 'safer bash' idiom 'set -euo pipefail' is the most common use — it turns on error-exit, unset-variable-error, and pipeline failure propagation.
local
Declares a variable scoped to the current function, hiding any global of the same name during the function's execution. Accepts the same attribute flags as declare (-a, -A, -i, -r, -n, ...).
shift
Renames positional parameters: $2 becomes $1, $3 becomes $2, and so on, decrementing $#. Optional N shifts by N positions and is common in manual argument parsing loops.