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?

  • As you tag ubuntu, you can read a good reference here – cuonglm Apr 16 '15 at 12:15
  • Yes, they are only in memory unless they've been intentionally saved somewhere -- which some of them usually are. The shell has a number of files it will potentially source (see e.g. INVOCATION in man bash). – goldilocks Apr 16 '15 at 12:24
  • @goldilocks aren't they stored somewhere in /proc/PID/? I thought so but couldn't find them. – terdon Apr 16 '15 at 13:01
  • @terdon Nothing's stored anywhere in /proc. It's an interface to the kernel, which manages the process images. When you read a file there, it amounts to asking the kernel for information, which it retrieves from memory and provides. – goldilocks Apr 16 '15 at 13:03
  • @goldilocks ah, so it might be accessible via /proc but not stored there. Thanks. – terdon Apr 16 '15 at 13:06

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.

  • 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: 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. – roaima 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? – roaima 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

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.

  • 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

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.

  • Thanks, I didn't know about /proc/$$/environ . Is there a pager for null-terminated files? – TomRoche Jun 20 '19 at 19:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.