1

If I export a variable in my terminal e.g.

$ export MY_VARIABLE=HelloWorld

where is it saved? Can I find it in the file system?

Bayou
  • 3,054
  • 1
  • 6
  • 20
Xaver Fleer
  • 165
  • 1
  • 1
  • 9
  • 2
    Memory. It's in a psuedo-filesystem `/proc//environ` in Linux. – jordanm May 07 '20 at 15:19
  • https://unix.stackexchange.com/a/91284/13796 – jordanm May 07 '20 at 15:20
  • It's not *in* the pseudo-filesystem so much as the pseudo-filesystem provides access to it. Each process's environment is just stored in memory in the process's address space. – chepner May 07 '20 at 16:12
  • Also, `/proc` is irrelevant here as macOS doesn't *have* a procfs. – chepner May 07 '20 at 16:14
  • I think you're misunderstanding what `export` does -- it doesn't "save" the variable at all, it just puts it in that shell process's environment, so it'll be inherited by programs that that shell launches. It has no affect on other running shells, programs launched other ways, and isn't permanent past when you exit that particular shell process. – Gordon Davisson May 07 '20 at 16:42
  • @GordonDavisson your comment revealed my misconception. I now understand. If you add this as comment I'll accept it. – Xaver Fleer May 25 '20 at 11:10

2 Answers2

2

I think you're misunderstanding what export does -- it doesn't "save" the variable at all, it just puts it in that shell process's environment, so it'll be inherited by programs that that shell launches. It has no affect on other running shells, programs launched other ways, and isn't permanent past when you exit that particular shell process.

Here's an example. The curl program will use proxies specified in environment variables like http_proxy. Suppose I do:

http_proxy=http://proxy.example.net/    # This won't work
curl http://www.example.com/

In this case, the http_proxy variable is only defined in the shell, and not inherited by the curl program, so it has no effect. On other other hand, if I run either:

export http_proxy=http://proxy.example.net/
curl http://www.example.com/

or

http_proxy=http://proxy.example.net/
export http_proxy
curl http://www.example.com/

...the export converts http_proxy into an environment variable, so it'll be inherited by the curl command (and other programs run by that shell), so curl will use that proxy.

But that's all export does! An exported variable has no presence outside of the process it's defined in, and subprocesses of that. It's not passed "up" the process tree to the process that launched the shell, or to any other shells or independent processes, and isn't really "stored" anywhere (other than the process data in memory).

People sometimes ask about setting an environment variable "permanently", but there isn't any such thing (at least in unix-like OSes, including macOS and Linux). The closest you can come is to add commands to set (and export) the variable in your shell startup files (maybe ~/.profile, ~/.bash_profile, ~/.bashrc, ~/.zprofile, ~/.zshrc, etc depending on which shell you use). This doesn't really make it permanent, it just re-creates it for each new shell process. (And even then, it won't have any presence in non-shell processes, like most cron jobs.)

Gordon Davisson
  • 107,068
  • 14
  • 108
  • 138
  • Don't know OP's mind, but in a sense, I think everything that is usable after a small amount of time is saved. But in many cases they are saved in memory not permanently and uniquely, like programming variables. – KeitelDOG Apr 08 '22 at 18:52
1

If you're looking for a way to list your environment variables you can use printenv