Unset all environment variables linux

How do I delete an exported environment variable?

Before installing gnuplot, I set the environment variable GNUPLOT_DRIVER_DIR = /home/gnuplot/build/src . During the installation, something went wrong. I want to remove the GNUPLOT_DRIVER_DIR environment variable. How can I achieve it?

For those looking for how to do this in Fish shell see stackoverflow.com/questions/30703860/… (even though this question isn’t for a specific shell)

7 Answers 7

unset is the command you’re looking for.

but this only works for a session, what about unsetting it definitely? or maybe searching where is the variable set, so you can go and delete it?

This should work per terminal instance. Generally, each time a terminal window is opened, it will load up variables from various places such as ~/.bashrc, ~/.profile, etc. Any variables you set in one terminal instance will not carry over to another. If you have a variable which seems to be set automatically every time you open terminal, try looking through the various hidden files in your home directory for it. Or, to see where it is being set, try «grep -r ~» where is the name of the variable. This may take a while if you have a lot of files in your home directory.

This removes the variable from the shell too though. Is the only way to unexport to do T=»$MYVAR»; unset MYVAR; MYVAR=»$T»; unset T ?

@olejorgenb At least in bash, you can say declare +x MYVAR to remove the export but keep the value in the current shell.

Walkthrough of creating and deleting an environment variable in Bash:

Test if the DUALCASE variable exists (empty output):

It does not, so create the variable and export it:

DUALCASE=1 export DUALCASE 

Check if it is there:

It is there. So get rid of it:

Check if it’s still there (empty output):

The DUALCASE exported environment variable is deleted.

Extra commands to help clear your local and environment variables:

Unset all local variables back to default on login:

CAN="chuck norris" set | grep CAN 
env | grep CAN # Empty output exec bash set | grep CAN env | grep CAN # Empty output 

exec bash command cleared all the local variables, but not environment variables.

Unset all environment variables back to default on login:

export DOGE="so wow" env | grep DOGE 
env -i bash env | grep DOGE # Empty output 

env -i bash command cleared all the environment variables to default on login.

maybe echo $VARIABLE is better than env | grep VARIABLE , it’s lighter as it doesn’t need to print all variables and then send its output to another (grep) process. Plus, env | VARIABLE could catch more than one variable that matches the same pattern. Plus2, echo $VARIABLE makes possible to complete variable’s name by hitting (if it exists, that also may be a hint to what you wanna do).

Читайте также:  Установка arch linux 2021

@RodrigoGurgel, echo $VARIABLE doesn’t tell you whether the VARIABLE is a shell variable (here called «local variable») or an environment variable, which is the whole point of the walkthrough.

Note that env -i bash seems to be creating a subshell (at least on a Mac) which may have unintended consequences.

@RodrigoGurgel using echo won’t show existing variable set to empty string or nul. to your point, though, a proper way to test for variable would be env | grep -e ‘^VARNAME=’ .

The original question doesn’t mention how the variable was set, but:

In C shell (csh/tcsh) there are two ways to set an environment variable:

The difference in the behaviour is that variables set with the setenv command are automatically exported to a subshell while variables set with set aren’t.

To unset a variable set with set, use

To unset a variable set with setenv, use

Note: in all the above, I assume that the variable name is ‘x’.

export GNUPLOT_DRIVER_DIR= 

The variable still exists, but it contains an empty string, as you can see in the output of the env command. It just might be the case that the application that uses the variable does not distinguish between non-existent and empty environment variable.

yes it will contain, this was just to remove value not to remove variable. But yes one can use — unset GNUPLOT_DRIVER_DIR.

This doesn’t work in the case of the PAGER variable. I tried to unset my PAGER setting with export PAGER= , but that just disabled paging entirely—all my man pages just dumped straight to the terminal. unset PAGER did the trick, reverting it to default behaviour.

Perhaps it is time to update your answer? (But without «Edit:», «Update:», or similar — the question/answer should appear as if it was written today.)

As mentioned in the above answers, unset GNUPLOT_DRIVER_DIR should work if you have used export to set the variable. If you have set it permanently in ~/.bashrc or ~/.zshrc then simply removing it from there will work.

On Linux and macOS, you can use the command unset to remove an environment variable

Remove the variable permanently,

In Linux

You can edit your shell profile file, such as .bashrc or .bash_profile in the /etc/profile.d directory and remove the line that exports the variable.

Then, search for the line export GNUPLOT_DRIVER_DIR and delete it. Then save the file.

In Windows

use the setx command to delete an environment variable.

You can find more information about environment variables and how to manage them in the following links:

First find which script file defines and adds the variable to the environment.

Читайте также:  Kali linux nethunter возможности

Look in /etc files like profile, bash.bashrc, .bashrc, .bashrc_login, etc

And look in similarly named files in the user’s home directory.

If it’s not in any of those, it’s likely that it’s in some script file that was part of an installed package. For example, the package SDKMAN (for managing alternate SDK versions) creates a variable called DERBY_HOME. To find the script file creating it, apply the following search command to the /etc folder:

$ sudo egrep -lir THE_VAR_NAME /etc 

This should produce some output like:

/etc/profile.d/jdk.sh /etc/profile.d/jdk.csh 

The separate file jdk.csh is needed for the C-shell environment if users have defaulted to it.

Once found it is simply a matter of navigating to the folder containing the script files, in this case here /etc/profile.d/ and then editing the files (with admin permission), removing the variable assignments and saving:

$ cd /etc/profile.d/ $ sudo gedit jdk.sh $ sudo gedit jdk.sh 

Of course, in this case the package setting the env variable is in use so I kept it.

But if the package were not in use and the env vars dead weight to the startup process, then it should be deleted.

Источник

How to Set Environment Variables in Linux

In this tutorial, you will learn how to set environment variables in Ubuntu, CentOS, Red Hat, basically any Linux distribution for a single user and globally for all users. You will also learn how to list all environment variables and how to unset (clear) existing environment variables.

Environment variables are commonly used within the Bash shell. It is also a common means of configuring services and handling web application secrets.

It is not uncommon for environment specific information, such as endpoints and passwords, for example, to be stored as environment variables on a server. They are also used to set the important directory locations for many popular packages, such as JAVA_HOME for Java.

Setting an Environment Variable

To set an environment variable the export command is used. We give the variable a name, which is what is used to access it in shell scripts and configurations and then a value to hold whatever data is needed in the variable.

For example, to set the environment variable for the home directory of a manual OpenJDK 11 installation, we would use something similar to the following.

export JAVA_HOME=/opt/openjdk11

To output the value of the environment variable from the shell, we use the echo command and prepend the variable’s name with a dollar ($) sign.

And so long as the variable has a value it will be echoed out. If no value is set then an empty line will be displayed instead.

Unsetting an Environment Variable

To unset an environment variable, which removes its existence all together, we use the unset command. Simply replace the environment variable with an empty string will not remove it, and in most cases will likely cause problems with scripts or application expecting a valid value.

Читайте также:  Logitech quickcam linux drivers

To following syntax is used to unset an environment variable

For example, to unset the JAVA_HOME environment variable, we would use the following command.

Listing All Set Environment Variables

To list all environment variables, we simply use the set command without any arguments.

An example of the output would look something similar to the following, which has been truncated for brevity.

BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=([0]="0") BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_VERSINFO=([0]="2" [1]="8") BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="5" [1]="0" [2]="3" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu") BASH_VERSION='5.0.3(1)-release' COLUMNS=208 DIRSTACK=() EUID=1000 GROUPS=() HISTCONTROL=ignoreboth HISTFILE=/home/ubuntu/.bash_history HISTFILESIZE=2000 HISTSIZE=1000 HOME=/home/ubuntu HOSTNAME=ubuntu1904 HOSTTYPE=x86_64 IFS=$' \t\n' LANG=en_US.UTF-8 LESSCLOSE='/usr/bin/lesspipe %s %s' LESSOPEN='| /usr/bin/lesspipe %s' LINES=54

Persisting Environment Variables for a User

When an environment variable is set from the shell using the export command, its existence ends when the user’s sessions ends. This is problematic when we need the variable to persist across sessions.

To make an environment persistent for a user’s environment, we export the variable from the user’s profile script.

    Open the current user’s profile into a text editor

export JAVA_HOME=/opt/openjdk11

Adding the environment variable to a user’s bash profile alone will not export it automatically. However, the variable will be exported the next time the user logs in.

To immediately apply all changes to bash_profile, use the source command.

Export Environment Variable

Export is a built-in shell command for Bash that is used to export an environment variable to allow new child processes to inherit it.

To export a environment variable you run the export command while setting the variable.

export MYVAR="my variable value"

We can view a complete list of exported environment variables by running the export command without any arguments.

SHELL=/bin/zsh SHLVL=1 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.1pB5Pry8Id/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.48.2

To view all exported variables in the current shell you use the -p flag with export.

Setting Permanent Global Environment Variables for All Users

A permanent environment variable that persists after a reboot can be created by adding it to the default profile. This profile is loaded by all users on the system, including service accounts.

All global profile settings are stored under /etc/profile. And while this file can be edited directory, it is actually recommended to store global environment variables in a directory named /etc/profile.d, where you will find a list of files that are used to set environment variables for the entire system.

    Create a new file under /etc/profile.d to store the global environment variable(s). The name of the should be contextual so others may understand its purpose. For demonstrations, we will create a permanent environment variable for HTTP_PROXY.

sudo touch /etc/profile.d/http_proxy.sh
sudo vi /etc/profile.d/http_proxy.sh
export HTTP_PROXY=http://my.proxy:8080
export HTTPS_PROXY=https://my.proxy:8080
export NO_PROXY=localhost. 1,.example.com

Conclusion

This tutorial covered how to set and unset environment variables for all Linux distributions, from Debian to Red Hat. You also learned how to set environment variables for a single user, as well as all users.

Источник

Оцените статью
Adblock
detector