unset Command
Remove shell variables and functions from the environment
Syntax:
unset [-fv] [name ...]
Description
The unset command is a shell builtin that removes variables and functions from the shell environment. It can unset shell variables, environment variables, and shell functions. Once a variable or function is unset, it is no longer available in the current shell session.
Note: unset is a shell builtin command, meaning it's built into the shell itself rather than being an external program. The behavior may vary slightly between different shells (bash, zsh, etc.).
Options
| Option | Description |
|---|---|
-f |
Treat names as functions only |
-v |
Treat names as variables only (default) |
Examples
Unset a shell variable:
# Set a variable MY_VAR="Hello World" echo $MY_VAR # Output: Hello World # Unset the variable unset MY_VAR echo $MY_VAR # Output: (empty)
Unset multiple variables:
# Set multiple variables VAR1="value1" VAR2="value2" VAR3="value3" # Unset multiple variables at once unset VAR1 VAR2 VAR3 # Check if they're unset echo "VAR1: $VAR1, VAR2: $VAR2, VAR3: $VAR3" # Output: VAR1: , VAR2: , VAR3:
Unset environment variables:
# Set and export an environment variable export MY_ENV_VAR="environment value" env | grep MY_ENV_VAR # Output: MY_ENV_VAR=environment value # Unset the environment variable unset MY_ENV_VAR env | grep MY_ENV_VAR # Output: (no output)
Unset shell functions:
# Define a function
my_function() {
echo "This is my function"
}
# Call the function
my_function
# Output: This is my function
# Unset the function
unset -f my_function
# Try to call it again
my_function
# Output: bash: my_function: command not found
Check if variable exists before unsetting:
# Check if variable is set
if [ -n "${MY_VAR+set}" ]; then
echo "MY_VAR is set, unsetting it"
unset MY_VAR
else
echo "MY_VAR is not set"
fi
Unset with explicit options:
# Explicitly unset a variable unset -v MY_VARIABLE # Explicitly unset a function unset -f my_function # This prevents conflicts when names could be both variables and functions
Variable vs Function Disambiguation
When a name could refer to both a variable and a function, unset follows these rules:
Default behavior (variables first):
# Create both a variable and function with the same name
test_name="variable value"
test_name() { echo "function output"; }
# Without options, unset removes the variable first
unset test_name
echo $test_name # Output: (empty)
test_name # Output: function output
# To remove the function, use -f
unset -f test_name
test_name # Output: command not found
Common Use Cases in Scripts
Clean up temporary variables:
#!/bin/bash # Script that processes data # Set temporary variables TEMP_DIR="/tmp/processing" TEMP_FILE="$TEMP_DIR/data.tmp" PROCESS_ID=$$ # Do some processing... mkdir -p "$TEMP_DIR" echo "Processing data..." > "$TEMP_FILE" # Clean up variables when done unset TEMP_DIR TEMP_FILE PROCESS_ID
Reset configuration variables:
#!/bin/bash
# Configuration script
# Set default values
CONFIG_FILE="/etc/myapp.conf"
DEBUG_MODE="false"
LOG_LEVEL="info"
# Load user configuration
if [ -f "$HOME/.myapprc" ]; then
source "$HOME/.myapprc"
fi
# Function to reset to defaults
reset_config() {
unset CONFIG_FILE DEBUG_MODE LOG_LEVEL
CONFIG_FILE="/etc/myapp.conf"
DEBUG_MODE="false"
LOG_LEVEL="info"
}
Conditional variable management:
#!/bin/bash
# Only set variable if not already set
if [ -z "$MY_PATH" ]; then
MY_PATH="/default/path"
fi
# Later, conditionally unset
if [ "$RESET_PATHS" = "true" ]; then
unset MY_PATH
fi
Function cleanup in libraries:
#!/bin/bash
# Library script with cleanup function
# Define utility functions
_internal_helper() {
echo "Internal helper function"
}
public_function() {
_internal_helper
echo "Public function"
}
# Cleanup function to remove internal functions
cleanup_library() {
unset -f _internal_helper
# Keep public_function available
}
Testing Variable State
Check if variable is set:
# Different ways to test if variable is set
VAR="some value"
# Method 1: Using parameter expansion
if [ -n "${VAR+set}" ]; then
echo "VAR is set"
fi
# Method 2: Using declare (bash specific)
if declare -p VAR &>/dev/null; then
echo "VAR is declared"
fi
# Unset and test again
unset VAR
if [ -z "${VAR+set}" ]; then
echo "VAR is not set"
fi
Distinguish between unset and empty:
# Set variable to empty string
EMPTY_VAR=""
# Test different states
if [ -n "${EMPTY_VAR+set}" ]; then
if [ -z "$EMPTY_VAR" ]; then
echo "EMPTY_VAR is set but empty"
else
echo "EMPTY_VAR is set and has value: $EMPTY_VAR"
fi
else
echo "EMPTY_VAR is not set"
fi
# Unset the variable
unset EMPTY_VAR
# Test again
if [ -z "${EMPTY_VAR+set}" ]; then
echo "EMPTY_VAR is now unset"
fi
Advanced Usage
Unset arrays (bash):
# Create an array
declare -a my_array=("one" "two" "three")
echo "${my_array[@]}"
# Output: one two three
# Unset entire array
unset my_array
echo "${my_array[@]}"
# Output: (empty)
# Unset specific array element
declare -a my_array=("one" "two" "three")
unset my_array[1]
echo "${my_array[@]}"
# Output: one three
Unset in different scopes:
#!/bin/bash
# Global variable
GLOBAL_VAR="global value"
function test_scope() {
local LOCAL_VAR="local value"
echo "In function: GLOBAL_VAR=$GLOBAL_VAR"
echo "In function: LOCAL_VAR=$LOCAL_VAR"
# Unset local variable
unset LOCAL_VAR
echo "After unset: LOCAL_VAR=$LOCAL_VAR"
# Unset global variable
unset GLOBAL_VAR
}
test_scope
echo "After function: GLOBAL_VAR=$GLOBAL_VAR"
Batch unset with patterns:
#!/bin/bash
# Set multiple variables with common prefix
TEMP_VAR1="value1"
TEMP_VAR2="value2"
TEMP_VAR3="value3"
OTHER_VAR="other"
# Function to unset variables by pattern
unset_by_pattern() {
local pattern="$1"
local var_list=$(compgen -v | grep "^$pattern")
for var in $var_list; do
echo "Unsetting: $var"
unset "$var"
done
}
# Unset all TEMP_* variables
unset_by_pattern "TEMP_"
Shell-Specific Behavior
| Shell | Behavior Notes |
|---|---|
bash |
Supports -f and -v options, can unset array elements |
zsh |
Similar to bash, additional array handling features |
dash |
Basic unset functionality, limited options |
ksh |
Supports -f and -v, some unique behaviors |
fish |
Uses different syntax: set -e variable_name |
Common Use Cases
- Script cleanup: Removing temporary variables after use
- Security: Clearing sensitive data from memory
- Configuration management: Resetting configuration variables
- Function management: Removing utility functions after use
- Environment cleanup: Removing exported variables
- Debugging: Clearing variables to test default behaviors
- Library management: Cleaning up internal functions
- Memory management: Freeing up shell memory from large variables
Best Practices
- Use unset to clean up temporary variables in scripts
- Be explicit with -f or -v when names might conflict
- Check if variables are set before unsetting to avoid errors
- Use unset for security when handling sensitive data
- Consider the scope when unsetting variables in functions
- Document when and why variables are being unset
- Test scripts with unset variables to ensure proper error handling
- Use unset in cleanup functions for better script organization
Return Values
| Condition | Return Code |
|---|---|
| Success | 0 |
| Invalid option | 1 |
| Name is readonly | 1 |
| Name not found (usually ignored) | 0 |