Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Bash unset command

Reference for the Bash unset builtin: remove variables, array elements and functions, the -v, -f and -n options, and the difference from an empty value.

unset removes a variable or function from the current shell. After unset NAME the variable no longer exists: ${NAME-default} uses the default, set -u complains about it and ${NAME+x} expands to nothing. That is different from NAME="", which leaves an empty but existing variable.

The same command deletes array elements (unset arr[2]) and functions (unset -f name).

Terminal
unset [-fnv] NAME [NAME ...]

Try it

Bash Unset versus empty

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Arrays, functions and read-only variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
unset [-f] [-v] [-n] name [name ...]
Name Description
name Variable (default), array element (arr[i]), or function (with -f) to remove.
-v Treat each name as a variable. This is the default behavior when a variable exists.
-f Treat each name as a function.
-n Treat each name as a nameref and remove the reference itself, not the variable it points to.

Returns: 0, even when the name does not exist; non-zero when a name is read-only or not a valid identifier.

Errors and edge cases: unset on a read-only variable prints "cannot unset: readonly variable" and fails. Quote array subscripts (unset 'arr[1]'), otherwise the shell may treat arr[1] as a glob pattern and expand it against file names.

Usage notes

  • Unset is not empty. Use ${var+x} or [[ -v var ]] to test whether a variable exists, -z "$var" to test for empty or unset.
  • Order of lookup. Without -v or -f, unset name removes a variable if one exists, and only otherwise a function of that name. To remove both, run unset name; unset -f name.
  • Local scope. Inside a function, unset on a variable declared local removes the local one; in dynamic scoping this may make an outer variable of the same name visible again. Bash 5 adds localvar_unset to control this.
  • Environment. Unsetting an exported variable also removes it from the environment of later child processes. To remove it only for one command use env -u NAME command.
  • Special variables such as RANDOM, SECONDS, LINENO lose their special behavior if you unset them, and it does not come back by assigning again.
  • Associative arrays. unset 'map[key]' deletes one key; quote carefully if the key contains special characters.
  • Cleaning up in scripts. unset is useful to drop secrets after use (unset PASSWORD), though the value may remain in memory until overwritten and in the environment of already-started children.
  • POSIX. unset -v and unset -f are standard; -n and array-element removal are Bash/ksh extensions.