To undefine and unset a bash environment variable in Linux or Unix, you can use the unset
command.
The unset
command is used to remove a variable from the shell environment. When you unset a variable, the value of the variable is no longer available to the shell or any of the programs that are run from the shell.
To unset a variable, you can use the unset
command followed by the name of the variable. For example, to unset the MYVAR
variable, you can use the following command:
$ unset MYVAR
This will remove the MYVAR
variable from the shell environment.
Note that the unset
command only removes the variable from the current shell environment. If you want to remove the variable from the environment of all shells and programs that are run from the current shell, you can use the export
command to unset the variable. For example, to unset the MYVAR
variable in the environment of all shells and programs that are run from the current shell, you can use the following command:
$ export -n MYVAR
This will remove the MYVAR
variable from the environment of all shells and programs that are run from the current shell.
Note that you may need to have root privileges to unset environment variables that are set in system-wide configuration files. You can use the sudo
command to execute the unset
or export
command with root privileges.
You can also use the env
command to list the environment variables that are set in the current shell. To list the environment variables, you can use the following command:
$ env
This will list all of the environment variables that are set in the current shell. You can use this command to verify that the variable has been unset.