Remove specified shell variables or functions.
unset [-f] [-v] [-n] [name ...]
-f: Remove functions only.
-v: Remove variables only (excluding read-only variables).
-n: Remove the variable name with the nameref attribute (if the option exists).
name (optional): The variable or function to be removed.
Returns success unless an option is invalid or the variable/function to be removed has the read-only attribute.
# Remove a variable.
declare paper_size='B5'
unset -v paper_size
# Remove a function.
function show_result(){ echo "Last Command Return: $?"; }
unset -f show_result
# When no options are specified, variables are prioritized; if that fails, functions are removed.
declare -i aa=100
function aa(){ echo 'aa'; }
unset aa
# Variable 'aa' has been removed.
declare -p aa
# Function 'aa' still exists.
declare -F | grep aa
# Demonstrating unset with the -n option when name specifies a reference variable.
declare a=3
# Define a reference variable
declare -n b=a
# Check attributes; shows declare -n b="a"
declare -p b
# Displays 3
echo ${b}
# Displays a
echo ${!b}
# When the -n option is specified
unset -n b
# Reference variable b has been removed
declare -p b
# Referenced variable a has not been removed
declare -p a
# Demonstrating unset without the -n option when name specifies a reference variable.
declare a=3
# Define a reference variable
declare -n b=a
# Check attributes; shows declare -n b="a"
declare -p b
# Displays 3
echo ${b}
# Displays a
echo ${!b}
# When the -n option is not specified
unset b
# Reference variable b is not removed; shows declare -n b="a"
declare -p b
# Referenced variable a is removed
declare -p a
help command.