LinuxCommandLibrary

unset

Remove shell variables

TLDR

Remove the variable foo, or if the variable doesn't exist, remove the function foo

$ unset [foo]
copy

Remove the variables foo and bar
$ unset -v [foo] [bar]
copy

Remove the function my_func
$ unset -f [my_func]
copy

SYNOPSIS

unset [-fv] name...

PARAMETERS

-f
    Removes the function named name. If the -f option is not specified, the variable named name is unset.

-v
    Treat each name as a variable; this is the default. Present for POSIX compliance, but generally unneeded.

name
    The name of the variable or function to unset.

DESCRIPTION

The unset command in Linux is used to remove shell variables or functions. When a variable is unset, it is removed from the shell's environment, and its value is no longer accessible. If the variable was exported to child processes, unsetting it will also prevent it from being inherited. unset can also be used to remove shell functions, effectively making them undefined in the current shell environment.

It's important to understand that unset only affects the current shell session and any child processes created after the command is executed. Variables or functions defined in the parent environment or in configuration files will persist until they are explicitly unset there. Using unset on read-only variables will result in an error message.

CAVEATS

unset cannot remove environment variables that are marked as read-only. Attempting to do so will result in an error. Also, unsetting a variable does not affect its value in other processes unless those processes inherit the environment after the unset command is executed.

ERROR HANDLING

If you try to unset a variable that doesn't exist, unset typically won't produce an error. However, attempting to unset a read-only variable will return an error message.

SEE ALSO

export(1), set(1), readonly(1), env(1)

Copied to clipboard