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
| Option | Description |
|---|---|
-f | Treat names as functions |
-v | Treat 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
| Status | Condition |
|---|---|
| 0 | Success |
| 1 | Invalid option |
| 1 | Attempting 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
unsetdoes 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