unset - Linux


The unset command in Linux is used primarily to unset or remove variables and functions from the environment. This can clear space in the environment or prevent scripts from accessing certain variables and functions, enhancing script robustness and security.


The basic syntax for the unset command is:

unset [-fv] [name ...]
  • -f: Unset a function.
  • -v: Unset a variable. This is the default behavior if no option is specified.

Multiple names can be provided as arguments, and each will be unset based on the option chosen.


  • -f: Use this option to specify that the names following it are functions that should be unset.
  • -v: Explicitly marks the names as variables. While this is the default behavior, using this flag can make scripts clearer.


  1. Unsetting an Environment Variable:

    unset varName

    Removes varName from the environment, making it unavailable in the current session.

  2. Unsetting Multiple Variables:

    unset var1 var2 var3

    This will unset var1, var2, and var3 simultaneously.

  3. Unsetting a Function:

    unset -f myFunc

    Unsets the function myFunc, so it is no longer defined or available.

Common Issues

  • Using Unset on Non-existent Names: If you attempt to unset a variable or function that does not exist, no error is displayed, which might be misleading in scripts expecting certain behaviors when variables exist.
  • Read-Only Variables: Attempting to unset a read-only variable results in an error. Ensure variables are not set to read-only if you plan to unset them.


unset can be combined with other Bash scripting commands for more dynamic and environment-sensitive scripts. For example, before sourcing a configuration script, you might want to unset existing configuration variables to prevent conflicts:

unset oldConfig
source newConfig.sh

This ensures that oldConfig does not affect the environment settings applied by newConfig.sh.

  • export: Used to set variables and mark them for export to the environment.
  • env: Displays the environment and can also be used to run commands in a modified environment.
  • set: Displays or sets shell attributes and positional parameters.

For further reading and more detailed information on the unset command, consult the Bash man page (man bash) or the online GNU Bash documentation.