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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesunset
Internet RequiredNo
Discussed Tools in this Article

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
declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:progcomp:promptvars:sourcepath"
declare -ar BASH_VERSINFO=([0]="5" [1]="1" [2]="16" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
declare -ir EUID="1003"
declare -ir PPID="26144"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
declare -ir UID="1003"

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

$ unset BASHOPTS
bash: unset: BASHOPTS: cannot unset: readonly variable

Also Read: How to Set and List Environment Variables in Linux

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
linuxtldr.com

#Deleting the variable
$ unset site
$ echo $site
bash: site: unbound variable

Removing variables created with the export command:

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

#Deleting the variable
$ unset site
$ echo $site
bash: site: unbound variable

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
Hi, Linux TLDR

#Delete the function
$ unset myfunc
$ myfunc
myfunc: command not found

Removing functions created with the export command:

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

#Delete the function
$ unset myfunc
$ myfunc
myfunc: command not found

Also Read: What is Exit Status Code ($?) in Linux

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.