unset

>Usage

The unset command in Unix is used to remove or clear shell variables and functions.

It deletes a specified variable or function from the environment, making it no longer accessible in the current shell session.

>Synopsis

unset [option] [variable/function-name]

>Options

-f

Removes a function definition rather than a variable. This is useful if you've defined a function in the shell and want to remove it.

Example:

Dots

-zsh

> unset -f my_function


Trying to call my_function after it has been unset results in the following error:

my_function: command not found

The function is no longer defined in the current shell session.

-v

Tells the shell to remove a variable. This option is often used to clarify that you’re deleting a variable rather than a function.

Example:

Dots

-zsh

> unset -v my_variable


Trying to call my_variable after it has been unset results in no output or an empty value.

-x

It will remove the variable from the environment of the current shell session, making it unavailable to subprocesses.

Example:

Dots

-zsh

> unset -x my_variable


Without unset -x: removes the variable but it might still exist locally within the shell session.

With unset -x: ensures the variable is completely removed from both the current shell and the environment of any subprocesses.

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Alessandro Tognoni