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.
Further Reading
- Bash Cookbook by Carl Albing, J.P. Vossen
- Wicked Cool Shell Scripts by Dave Taylor, Brandon Perry
- Black Hat Bash by Nick Aleks, Dolev Farhi
- Bash Pocket Reference by Arnold Robbins
- The Linux Command Line by William Shotts
- Learning the Bash Shell by Cameron Newham
- Mastering Linux Shell Scripting by Mokhtar Ebrahim, Andrew Mallett
- Linux Command Line and Shell Scripting Bible by Richard Blum, Christine Bresnahan
- Shell Scripting by Jason Cannon