Beginners Guide for Unset Command in Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

The unset command is a built-in Linux command used for flushing the value of variables or functions during program execution.

Tutorial Details

DescriptionUnset
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesunset
Internet RequiredNo

Syntax of the Unset Command

The unset command takes two arguments: one is the option, and the other is the name of the variable or function.

$ unset [OPTION] [VARIABLE]

The following is a list of valid options:

OptionsDescription
-fRemove the specified function.
-vRemove the specified variable.
-nUnset the variable itself rather than the variable it references.

When the set command is executed without specifying the option, it will first try to remove a variable, and if that fails, it will try to unset a function.

Note that you cannot unset the value of “readonly” variables or functions.

Execute the following command to get the complete list of “readonly” variables and functions.

$ readonly

Output:

readonly command output

If you try to remove any variable or function from the above list, it will give you the following error:

$ unset BASHOPTS

Output:

Trying to unset readonly variable value

Removing Variables Using the Unset Command

The variable created with or without the export command can be deleted using the unset command with or without specifying the “-v” flag.

Removing variables created without the export command:

#Creating a variable
$ site=linuxtldr.com
$ echo $site

#Deleting the variable
$ unset site
$ echo $site

Output:

Creating variable then accessing it and then removing and retrying to access it

Removing variables created with the export command:

#Creating a variable
$ export site=linuxtldr.com
$ echo $site

#Deleting the variable
$ unset site
$ echo $site

Output:

Creating variable using export command then accessing it and then removing and retrying to access it

Removing Functions Using the Unset Command

The functions can also be deleted, just like variables, whether created with or without the export command.

However, if you do not specify the “-f” flag, it will first look at it as a variable and try to delete it; if that fails, it will treat it as a function.

Removing functions created without the export command:

#Creating a function
$ myfunc() { echo "Hi, Linux TLDR"; }
$ myfunc

#Delete the function
$ unset myfunc
$ myfunc

Output:

Creating function then accessing it and then removing and retrying to access it

Removing functions created with the export command:

#Creating a function
$ myfunc() { echo "Hi, Linux TLDR"; }
$ export -f myfunc
$ myfunc

#Delete the function
$ unset myfunc
$ myfunc

Output:

Creating function using export command then accessing it and then removing and retrying to access it

Exit Status

The unset command has three exit statuses:

  • 0: Successful execution.
  • 1: The assigned variable is read-only.
  • 2: An invalid option is provided.

And that was the last part of this article.

There is not enough to learn about this topic. However, if you think I missed something, feel free to point it out in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.