Walker News

How To Remove Linux Environment Variable Or Shell Script Variable?

On Linux system that uses Bash shell, you can use unset command to destroy or remove a shell script variable or environment variable.

Why there is a need to remove or destroy the shell script variable or environment variable?

Shell script variable

If so happen a variable that defined and referred by a shell script remains existing in the Bash shell execution space, that shell script might return unexpected result on next execution if it refers to the variable containing old value left by previous execution.

Therefore, it is recommended to unset variables used in a Bash shell script before using the variables (preferable at the very first beginning of shell script).

Environment variable

Some Linux commands behave differently if a corresponding environment variable exists. For example, the TIMEFORMAT environment variable can override time format returned by the time command (of Bash shell).

After defining TIMEFORMAT, you cannot set this environment variable to blank / null / empty. Rather, you have to remove or destroy this environment variable, as if it is never defined (by you), then only the time command will output result correctly (in its default format).

How to use the unset command?

For example, to remove TIMEFORMAT from Bash shell, type this at Linux command prompt:

Uses unset command to remove or delete environment variable or shell script variable from Bash shell.

Similarly for shell script, write unset <variable_name> to remove the variable called variable_name before using it.

Custom Search

2016  •  Privacy Policy