Linux delete export variable

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).

Читайте также:  Running samba on linux

@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.

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.

Читайте также:  How to install fonts on linux

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.

Источник

Un-exporting an Exported Variable in Bash in Linux

but I want to unexport the MODE (not turn to be `””`, should be truly non-defined).

For your reference (from the [`bash` manual](https://www.systutorials.com/docs/linux/man/1-bash/)):

export [-fn] [name[=word]] . export -p The supplied names are marked for automatic export to the en‐ vironment of subsequently executed commands. If the -f op‐ tion is given, the names refer to functions. If no names are given, or if the -p option is supplied, a list of names of all exported variables is printed. The -n option causes the export property to be removed from each name. If a variable name is followed by =word, the value of the variable is set to word. export returns an exit status of 0 unless an in‐ valid option is encountered, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a function.
unset [-fv] [-n] [name . ] For each name, remove the corresponding variable or function. If the -v option is given, each name refers to a shell variable, and that variable is removed. Read-only variables may not be unset. If -f is speci‐ fied, each name refers to a shell function, and the function definition is removed. If the -n option is supplied, and name is a variable with the nameref attribute, name will be unset rather than the variable it references. -n has no effect if the -f option is supplied. If no options are supplied, each name refers to a variable; if there is no variable by that name, any function with that name is unset. Each unset variable or function is removed from the envi‐ ronment passed to subsequent commands. If any of COMP_WORDBREAKS, RANDOM, SECONDS, LINENO, HISTCMD, FUNCNAME, GROUPS, or DIRSTACK are unset, they lose their special properties, even if they are subse‐ quently reset. The exit status is true unless a name is readonly.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric’s own and do not necessarily represent those of any third parties.

One comment:

See
help export
export: export [-fn] [name[=value] …] or export -p
Set export attribute for shell variables.

Marks each NAME for automatic export to the environment of subsequently
executed commands. If VALUE is supplied, assign VALUE before exporting.

Options:
-f refer to shell functions
-n remove the export property from each NAME
-p display a list of all exported variables and functions

Читайте также:  Linux compare file directories

An argument of `–‘ disables further option processing.

Exit Status:
Returns success unless an invalid option is given or NAME is invalid.

Источник

linux — [Solved-2 Solutions] How to delete (unset) an exported environment variable ? — ubuntu — red hat — debian — linux server — linux pc

How to delete (unset) an exported environment variable ?

Linux — Solution 1:

click below button to copy the code. By — Linux tutorial — team

Linux — Solution 2:

Creating and deleting an environment variable in bash:

Test if the DUALCASE variable exists:

 [email protected]:~$ env | grep DUALCASE [email protected]:~$ 
click below button to copy the code. By — Linux tutorial — team

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

 [email protected]:~$ DUALCASE=1 [email protected]:~$ export DUALCASE
click below button to copy the code. By — Linux tutorial — team
 [email protected]:~$ env | grep DUALCASE DUALCASE=1
click below button to copy the code. By — Linux tutorial — team
click below button to copy the code. By — Linux tutorial — team
 [email protected]:~$ env | grep DUALCASE [email protected]:~$ 
click below button to copy the code. By — Linux tutorial — team

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:

 [email protected]:~$ CAN="wikitechy" [email protected]:~$ set | grep CAN CAN='wikitechy' [email protected]:~$ env | grep CAN [email protected]:~$ [email protected]:~$ exec bash [email protected]:~$ set | grep CAN [email protected]:~$ env | grep CAN [email protected]:~$
click below button to copy the code. By — Linux tutorial — team

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

Unset all environment variables back to default on login:

 [email protected]:~$ export DOGE="welcome to wikitechy" [email protected]:~$ env | grep DOGE DOGE=welcome to wikitechy [email protected]:~$ env -i bash [email protected]:~$ env | grep DOGE [email protected]:~$
click below button to copy the code. By — Linux tutorial — team

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

linux red hat debian opensuse ubuntu arch linux mandrake get link linux computer linux pc linux server linux desktop learn linux red hat linux red hat enterprise linux linux software linux tutorial linux operating system suse linux linux download linux os linux ubuntu vmware linux lunix linux windows linux news linux usb linux commands unix linux linux version what is linux linux centos linux ftp how to delete environment variable in linux bash unset all environment variables unset environment variable windows delete environment variable ubuntu unset variable bash unset environment variable csh bash unset variable in script unset environment variable python

    INTERVIEW TIPS
  • Final Year Projects
  • HR Interview Q&A
  • GD Interview
  • Resume Samples
  • Engineering
  • Aptitude
  • Reasoning
  • Company Questions
  • Country wise visa
  • Interview Dress Code CAREER GUIDANCE
  • Entrance Exam
  • Colleges
  • Admission Alerts
  • ScholarShip
  • Education Loans
  • Letters
  • Learn Languages

World’s No 1 Animated self learning Website with Informative tutorials explaining the code and the choices behind it all.

Источник

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