6

If I do not edit the .bashrc or other config files, the environment variables that I've setted are gone when I logout, or turn off the terminal.

What I'm curious is, where are those 'temporary' env vars saved in?

As I guess, they might be in the memory. That makes sense because they will disappear when the terminal is turned off(equals the terminal I was using is gone from the memory). Am I correct?

terdon
  • 242,166

3 Answers3

8

Environment variables are stored in memory associated with a process.

Every process has access to its own set of environment variables. A child process (one started by the "current" process) inherits a copy of those variables. It's not possible for any process to alter any other process's environment variables.

Using a shell such as bash you can define environment variables when you log in, or start a new bash process. The shell itself also defines a number of environment variables (PWD springs to mind, after being prompted by comments), and other environment variables, such as PATH, are used at a much deeper level that just the shell - in this example by the system libraries.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 2
    This is correct. The copy comes from the fork() system call, which creates a new process by duplicating the one that started it. That process image is then largely replaced by the exec() system call, but the environment variables are retained. Some other details here: http://unix.stackexchange.com/a/123390/25985 – goldilocks Apr 16 '15 at 12:22
  • @Goldilocks that's a good cross-reference; thank you for the addition. – Chris Davies Apr 16 '15 at 12:43
  • IFS is not really an environment variable. There are lots of others to choose from though — maybe PWD instead? – Jonathan Leffler Apr 16 '15 at 14:00
  • @JonathanLeffler perhaps IFS wasn't one of the better ones, but if it walks like a duck, quacks like a duck... I am intrigued to understand why you would prefer PWD to IFS, though. One is exported by default and one isn't, but other than that they seem to have the same status? – Chris Davies Apr 16 '15 at 14:07
  • 1
    Environment variables are exactly and only those shell variables that are exported and hence available in sub-processes such as env. Other (unexported) variables are not environment variables; they're just shell variables. And IFS is unusual in that the shell resets it to a default value at startup even if it is exported. Other programs get to treat it like an ordinary environment variable if (and only if) it is exported. – Jonathan Leffler Apr 16 '15 at 14:10
2

Strictly speaking, environment variables aren't "saved" as we tend to think of saving. They exist in the memory of a process. They're created when a process starts (possibly as a copy of the environment variables from the calling process)

In Linux, you actually can get them as a "file" of sorts, if you know the PID of the process you want the environment variables for. They're in /proc/<pid>/environ.

In bash, you can get your PID from the environment variable $, so the file you'd want is /proc/$$/environ. They're stored as name=value pairs, with a null character between them. This isn't really a file (and you can't write to it), but you can access it as though it were a file, because sometimes it's convenient to do that.

guntbert
  • 1,637
1

Yes and no (more yes than no, though). They are in your shell's memory, so not only do they go away when your current shell exits, they will not be there if you open a new shell anywhere other than your current shell.

John
  • 17,011
  • I disagree with this answer, but not enough to downvote it. The answer is not "Yes and no", it's clearly "yes". And they are not only in "your shell's memory", they are in every process' memory. And the fact that a new shell will not have them is neither necessarily true (the other shell could be a child of the first shell) nor relevant to the question. – Celada Apr 16 '15 at 12:22
  • 1
    John does state if you open a new shell anywhere other than your current shell – wurtel Apr 16 '15 at 12:30