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.

unset Syntax:

Terminal window
unset [option] [parameter]

Options:

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.

Parameters:

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.

unset Command Samples:

Unset an Environment Variable

Terminal window
unset VARIABLE_NAME

Removes the specified environment variable from the current environment.

Unset Multiple Environment Variables

Terminal window
unset VAR1 VAR2 VAR3

Unsets multiple environment variables in a single command.

Unset All Variables Starting with a Prefix

Terminal window
unset VAR_PREFIX*

Removes all environment variables that start with a specific prefix.

Unset a Function

Terminal window
unset -f FUNCTION_NAME

Deletes the specified function from the current shell session.

Unset a Readonly Variable

Terminal window
unset READONLY_VAR

Can unset a variable that was previously set as readonly.

Unset a Variable in a Subshell

Terminal window
(subshell) unset VAR_IN_SUBSHELL

Unset a variable within a subshell environment.

Unset All Variables

Terminal window
unset *

Removes all variables and functions from the current shell session.

unset FAQ:

How do I use unset in Linux?

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

Terminal window
unset variable_name

What does unset do in Linux?

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

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?

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

Terminal window
unset -f function_name

How can I unset all variables in Linux?

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?

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

Terminal window
unset -f readonly_variable

Applications of the unset command

  • Clearing environment variables
  • Removing elements from an array
  • Unsetting variables in a script or terminal session