84

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.

Ben L
  • 6,132
  • 8
  • 38
  • 34

7 Answers7

121

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.

Gilles 'SO- stop being evil'
  • 98,216
  • 36
  • 202
  • 244
Johannes Schaub - litb
  • 481,675
  • 123
  • 870
  • 1,191
  • cletus, yes i think it is linux specific, but i don't know. – Johannes Schaub - litb Feb 11 '09 at 05:48
  • @askveikau, note that I do not propose to use that file by a program to read environment variables. I showed that the linux kernel exports them into that file - no more and no less. That said, I don't know what guarantees the linux kernel makes with regard to that file. – Johannes Schaub - litb Oct 24 '10 at 10:42
  • 13
    The kernel does not store the environment variables, they are stored in user mode. /proc//environ may return the wrong values if the environment block has been re-allocated. – atomice Aug 08 '11 at 13:16
  • 2
    @atomice I was in the impression that the kernel stores them and makes them visible to user programs, writable to user programs. Doesthe `environ` file represent the wrong values if an environment variable was changed in a defined manner? Can you perhaps show a program that exploits that? – Johannes Schaub - litb Aug 08 '11 at 16:12
  • 16
    The kernel stores them at a particular location in user space and that is what /proc//environ exposes. However if you modify the environment in a program using putenv or setenv the initial environment block is liable to be re-allocated (to accomodate the new variable). The new variables won't show up in the output from /proc//environ. – atomice Sep 13 '11 at 09:06
  • 2
    @atomice So, where the new updated environment will stay? Which command to show it? – Antonio Rizzo Jan 29 '14 at 18:32
  • 1
    @atomice : at which virtual address are they stored then ? It’s in the case of x86_64. – user2284570 Feb 01 '17 at 00:58
  • @atomice aren't the new exported variables mapped in fs any where else? – Error Jun 21 '17 at 21:49
  • Great answer, but while this answer is a comprehensive explanation of environment variables in Linux, it totally misses the question IMHO, The question probably arouse in a scenario where the questioner tried to start an X client from another host (ssh) or from within a container, or something like that, and probably stumbled upon the famous DISPLAY variable mentioned everywhere, and asked the question, to find out how to set it up manually, being under the (probably wrong) impression, that this is required to achieve something else, like e.g. getting some X client to appear... – sheyll Dec 02 '21 at 09:26
  • 2
    For user2284570's question. 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. – StndFish Jan 07 '22 at 22:52
41

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)

tddmonkey
  • 20,132
  • 9
  • 55
  • 67
15

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

JohnnyQ
  • 4,467
  • 6
  • 42
  • 61
  • The question is about where in ʀᴀᴍ. – user2284570 Feb 03 '17 at 23:54
  • 10
    @user2284570 where in the question specified that? Can you please elaborate? – JohnnyQ Feb 09 '17 at 07:13
  • For user2284570's question. 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 – StndFish Jan 07 '22 at 22:55
11

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.

Svante
  • 49,447
  • 11
  • 79
  • 121
cletus
  • 599,013
  • 161
  • 897
  • 938
0

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

StndFish
  • 89
  • 5
-1

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.

Anish Jain
  • 431
  • 4
  • 8
  • -1: In any modern *nix there a _dozen_ places you can set env vars (`~/.profile`, `/etc/environment`, etc), `.bashrc` (actually `~/.bashrc`) being a really poor choice for that, as it is only read _by bash_, and only in _interactive_ and _non-login_ shells. – MestreLion May 27 '22 at 16:53
-8

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.