Skip to content

unset Linux Command Guide

The Linux unset command is used to remove environment variables. It allows you to unset a specific variable or multiple variables at once. When you unset a variable, its value and existence are deleted from the environment. This can help manage your environment and prevent potential conflicts or unwanted behavior in your shell sessions. Understanding how to use unset properly can help you maintain a clean and efficient working environment on your Linux system.

Terminal window
unset [option] [parameter]
OptionDescription
-vPrint each variable name before unset.
-fUnsets the function names only.
-nTreats each name as a variable name, even if it contains a symbol.
-aUnsets all variable names indexed by an array name.
ParameterDescription
nameSpecifies the name of the variable to unset.
-v nameUnsets the variable name.
-f nameUnsets the function name.
-n nameTreats the given name as a variable name.
-a nameUnsets all variables indexed by array name.
Terminal window
unset VARIABLE_NAME

Removes the specified environment variable from the current environment.

Terminal window
unset VAR1 VAR2 VAR3

Unsets multiple environment variables in a single command.

Unset All Variables Starting with a Prefix

Section titled “Unset All Variables Starting with a Prefix”
Terminal window
unset VAR_PREFIX*

Removes all environment variables that start with a specific prefix.

Terminal window
unset -f FUNCTION_NAME

Deletes the specified function from the current shell session.

Terminal window
unset READONLY_VAR

Can unset a variable that was previously set as readonly.

Terminal window
(subshell) unset VAR_IN_SUBSHELL

Unset a variable within a subshell environment.

Terminal window
unset *

Removes all variables and functions from the current shell session.

To use the unset command in Linux, execute the following command:

Terminal window
unset variable_name

The unset command in Linux is used to remove variables or functions.

How can I unset multiple variables in Linux?

Section titled “How can I unset multiple variables in Linux?”

To unset multiple variables in Linux, you can specify them separated by a space in a single unset command.

Terminal window
unset var1 var2 var3

Can unset be used to unset functions in Linux?

Section titled “Can unset be used to unset functions in Linux?”

Yes, the unset command can be used to unset functions in Linux.

Terminal window
unset -f function_name

To unset all variables in Linux, you can use the following command:

Terminal window
unset $(compgen -v)

Is there a way to unset a readonly variable in Linux?

Section titled “Is there a way to unset a readonly variable in Linux?”

Yes, you can unset a readonly variable in Linux by using the -f option.

Terminal window
unset -f readonly_variable
  • Clearing environment variables
  • Removing elements from an array
  • Unsetting variables in a script or terminal session