Linux: where are environment variables stored?

LinuxEnvironment Variables

Linux Problem Overview


If I type into a terminal,

export DISPLAY=:0.0

... where is the shell storing that environment variable?

I'm using Ubuntu 8.10. I've looked in the files ~/.profile and /etc/profile and can find no trace of DISPLAY.

Linux Solutions


Solution 1 - Linux

The environment variables of a process exist at runtime, and are not stored in some file or so. They are stored in the process's own memory (that's where they are found to pass on to children). But there is a virtual file in

/proc/pid/environ

This file shows all the environment variables that were passed when calling the process (unless the process overwrote that part of its memory — most programs don't). The kernel makes them visible through that virtual file. One can list them. For example to view the variables of process 3940, one can do

cat /proc/3940/environ | tr '\0' '\n'

Each variable is delimited by a binary zero from the next one. tr replaces the zero into a newline.

Solution 2 - Linux

Type set and you will get a list of all the current variables. If you want something to persist put it in ~/.bashrc or ~/.bash_profile (if you're using bash)

Solution 3 - Linux

If you want to put the environment for system-wide use you can do so with /etc/environment file.

Solution 4 - Linux

It's stored in the process (shell) and since you've exported it, any processes that process spawns.

Doing the above doesn't store it anywhere in the filesystem like /etc/profile. You have to put it there explicitly for that to happen.

Solution 5 - Linux

There is 1 file that can be used to store env variables.

> .bashrc

You can add your variables and use them. For example I have added Django virtual env as environment variable and now I can access it anywhere. Add this to your bashrc file

django_env='source/media/anish/Softwares/virtual_env/django2/bin/activate' 

now you need to restart your system to reflect changes and after restarting enter $django_env to start your virtual environment. as simple as that.

Solution 6 - Linux

As to the location of environment variables in RAM, they are stored in the top of the stack of main() function. any dynamic modification by setenv() et al. are then allocated elsewhere

Solution 7 - Linux

That variable isn't stored in some script. It's simply set by the X server scripts. You can check the environment variables currently set using set.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen LView Question on Stackoverflow
Solution 1 - LinuxJohannes Schaub - litbView Answer on Stackoverflow
Solution 2 - LinuxtddmonkeyView Answer on Stackoverflow
Solution 3 - LinuxJohnnyQView Answer on Stackoverflow
Solution 4 - LinuxcletusView Answer on Stackoverflow
Solution 5 - LinuxAnish JainView Answer on Stackoverflow
Solution 6 - LinuxStndFishView Answer on Stackoverflow
Solution 7 - LinuxEduard - Gabriel MunteanuView Answer on Stackoverflow