unset Command: Tutorial & Examples

Unset the value of an environment variable

n Linux, unset is a command used to remove a variable or function definition from the current shell environment. It is used to delete the value assigned to a variable, making it unset or undefined. Once a variable is unset, it can no longer be used in the current shell session, and its value cannot be accessed.

The basic syntax of the unset command is:

unset variable-name

For example, if you have a variable named x with the value of 5, you can use unset x to remove the variable and its value from the current shell session.

You can also use the -v option to unset a variable:

unset -v variable-name

You can also use unset -f to delete a function definition from the current shell environment:

unset -f function-name

It's important to note that once a variable or function is unset, it can no longer be accessed or used in the current shell session.

Also, if you are using this command inside a script, it will only delete the variable in the script's environment and not in the environment of the shell that calls the script.

It's a good practice to unset variables that you no longer need to free up memory and prevent confusion.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA