unset

Remove variables or functions.

Source: src/execution/builtins.f90

Synopsis

unset [-fv] name [name ...]

Description

The unset builtin removes shell variables or functions. Once unset, the name is no longer defined.

Options

OptionDescription
-fTreat names as functions
-vTreat names as variables (default)

Usage

Unset Variables

myvar="value"
echo $myvar    # value

unset myvar
echo $myvar    # (empty)

Unset Multiple

unset var1 var2 var3

Unset Functions

myfunc() { echo "hello"; }
myfunc         # hello

unset -f myfunc
myfunc         # command not found

Examples

Clear Configuration

# Reset to defaults
unset DEBUG VERBOSE CONFIG_PATH

Cleanup in Script

cleanup() {
    unset password secret_key api_token
}
trap cleanup EXIT

Remove Array Element

arr=(a b c d e)
unset 'arr[2]'       # Remove index 2
echo "${arr[@]}"     # a b d e

Remove Entire Array

declare -a myarray=(1 2 3)
unset myarray

Check and Unset

[[ -v myvar ]] && unset myvar

Behavior

Readonly Variables

readonly CONST=value
unset CONST
# Error: CONST is readonly

Unset in Functions

func() {
    local var="local"
    unset var      # Unsets local first
}

var="global"
func
echo $var          # global (still exists)

Environment Variables

export PATH="/custom:$PATH"
unset PATH
# PATH is now unset (not default)

Exit Status

StatusCondition
0Success
1Invalid option
1Attempting to unset readonly variable

Differences from Empty

var=""           # var is set to empty string
unset var        # var is unset (undefined)

# Test difference
[[ -v var ]]     # true if set (even if empty)
[[ -z "$var" ]]  # true if empty or unset

Common Patterns

Temporary Override

# Save, modify, restore
old_path="$PATH"
export PATH="/special:$PATH"
# ... do work ...
export PATH="$old_path"
unset old_path

Clean Environment

# Remove potentially problematic variables
unset LD_PRELOAD LD_LIBRARY_PATH

Function Cleanup

# Remove helper functions after use
_helper1() { ...; }
_helper2() { ...; }

main() {
    _helper1
    _helper2
}
main

unset -f _helper1 _helper2

Notes

  • unset does not affect exported status of other processes
  • Cannot unset $- or other special parameters
  • Unsetting positional parameters requires set --
  • Variable must exist; unsetting undefined variable succeeds silently

See Also