unset

shell builtinLinux/Unix
The unset command is one of the most frequently used commands in Linux/Unix-like operating systems. unset Remove variable or function names from the shell environment

Quick Reference

Command Name:

unset

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

unset [options] [arguments]

Common Use Cases

    Syntax

    unset [-fv] [name ...]

    Options

    Option Description
    -f Treat each NAME as a shell function
    -v Treat each NAME as a shell variable
    -n Treat each NAME as a name reference (nameref) variable (bash 4.3+)

    If neither -f nor -v is supplied, unset first tries to unset a variable, and if that fails, tries to unset a function.

    Examples

    How to Use These Examples

    The examples below show common ways to use the unset command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Set and then unset a variable
    MY_VAR="hello" echo $MY_VAR unset MY_VAR echo $MY_VAR # Nothing will be displayed
    # Define and then unset a function greet() { echo "Hello World"; } greet unset -f greet # greet will now give command not found error
    # Advanced Examples Advanced
    # Unset multiple variables at once
    A=1; B=2; C=3 echo $A $B $C unset A B C echo $A $B $C # Nothing will be displayed # Use unset to reset environment variables export CUSTOM_PATH="/custom/path:$PATH" echo $CUSTOM_PATH unset CUSTOM_PATH echo $CUSTOM_PATH # Nothing will be displayed # Check if variable exists before unsetting if [ -n "$MY_VAR" ]; then unset MY_VAR fi # Use unset in a script to clean up temporary variables TEMP_VAR1="temporary data" TEMP_VAR2="more data" # ... script logic here ... # Clean up at the end unset TEMP_VAR1 TEMP_VAR2 # Unset array elements # (Create array with elements 1, 2, 3, 4, 5) # Remove the third element (index 2) # Now array contains 1, 2, 4, 5 # Unset specific environment variable export TEST_ENV="test value" env | grep TEST_ENV unset TEST_ENV env | grep TEST_ENV # Nothing will be displayed # Conditionally unset variables if [ "$DEBUG" != "true" ]; then unset DEBUG_VAR1 DEBUG_VAR2 fi

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `unset` command is a shell builtin command in Unix-like operating systems that removes variable or function definitions from the current shell environment. This command is primarily used in shell scripts and interactive shell sessions to clean up the environment by removing variables or functions that are no longer needed. When a variable or function is unset, it's completely removed from the shell's environment, and any subsequent attempts to access it will treat it as if it had never been defined. This is different from simply assigning an empty value to a variable, which would keep the variable defined but with no content. **Key aspects of the `unset` command:** 1. **Variable Removal**: The primary use is to remove shell variables, whether they're local to a script or exported environment variables. This helps prevent variable name conflicts and reduces memory usage in long-running scripts. 2. **Function Removal**: With the `-f` option, `unset` can remove shell function definitions, which is useful for cleaning up temporary functions or redefining functions without causing warnings. 3. **Security Applications**: `unset` is often used in security-conscious scripts to remove sensitive information stored in variables once it's no longer needed, reducing the risk of exposure. 4. **Environment Cleaning**: It's commonly used to restore a clean environment by removing custom variables and functions before executing external programs that might be affected by them. 5. **Array Element Removal**: In shells like Bash, `unset` can remove specific elements from arrays using array indexing notation. **Behavior specifics:** - If no option is specified, `unset` first tries to unset a variable, and if that fails, attempts to unset a function with the same name. - The `-v` option explicitly specifies that names should be treated as variables. - The `-f` option explicitly specifies that names should be treated as functions. - In Bash 4.3 and later, the `-n` option allows unsetting of nameref variables (variable references). - Some variables are read-only or otherwise protected by the shell and cannot be unset (e.g., `BASH_VERSION`, `RANDOM`). - Unsetting a variable that doesn't exist is not considered an error. **Common use cases for `unset` include:** - Cleaning up temporary variables in scripts to prevent name collisions and reduce memory usage. - Ensuring a clean environment for external commands that might be affected by certain environment variables. - Managing the lifecycle of variables in complex scripts, particularly those that process many items in a loop. - Removing sensitive information from memory when it's no longer needed. - Controlling the visibility of functions or variables in different parts of a script. The `unset` command is supported in virtually all Unix-like shells, including Bash, Zsh, Ksh, and others, making it a portable and fundamental tool for shell scripting and interactive shell usage.

    Related Commands

    These commands are frequently used alongside unset or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the unset command works in different scenarios.

    $ unset
    View All Commands