The unset command is a built-in Linux command used for flushing the value of variables or functions during program execution.
Tutorial Details
| Description | Unset |
| Difficulty Level | Low |
| Root or Sudo Privileges | No |
| OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
| Prerequisites | unset |
| Internet Required | No |
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:
| Options | Description |
|---|---|
-f | Remove the specified function. |
-v | Remove the specified variable. |
-n | Unset 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.
$ readonlyOutput:

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

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 $siteOutput:

Removing variables created with the export command:
#Creating a variable
$ export site=linuxtldr.com
$ echo $site
#Deleting the variable
$ unset site
$ echo $siteOutput:

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
$ myfuncOutput:

Removing functions created with the export command:
#Creating a function
$ myfunc() { echo "Hi, Linux TLDR"; }
$ export -f myfunc
$ myfunc
#Delete the function
$ unset myfunc
$ myfuncOutput:

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.




