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

← All Bash builtins · Linux commands