4

I've been using Environment variables for ages but only just read about Environment variables vs. Shell variables. According to the definition:

  • Environment variables are "system-wide" and inherited by child shells and processes
  • Shell variables are only valid in the shell in which they are set

The book I'm reading goes on to state that using export takes a variable in your current environment (bash shell) and makes it available in any every environment until you change it again.

But if I change an Environment variable (HISTSIZE, for example) by calling export HISTZISE=999, and then check the value in a different terminal tab (echo $HISTSIZE), the change isn't visible. Why is this if the change is supposed to be system-wide? Personally I'm used to always setting my Environment variables when opening a new terminal tab, and I had taken it for granted that exporting values wouldn't affect other tabs (shells?).

So my question is: Are different terminal tabs considered different shell environments? And what does "system-wide" then mean?

Thanks for the help!

  • echo $$ shows you process identifier of the current shell. You can check that the value is different in different tabs. – Nykakin Apr 14 at 16:16
7

export exports a variable to all children of the current shell.

So if you do

somevariable=somevalue
export somevariable
bash 
echo $somevariable

you'll see the value of $somevariable in this new shell.

However shells in other terminal tabs are not children of the shell in the first tab, so they won't inherit the exported variables.

The shells in terminal tabs are all children of the gnome process that opened the terminal, so they are "brothers and sisters". You can verify this by calling ps -f in two terminal tabs and looking at the column PPID (parent process ID) of the bash line. In my example both have the parent 5319 which is the gnome terminal process.

tab 1:

$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
jean      5329  5319  0 10:36 pts/0    00:00:00 bash
jean      5359  5329  0 10:37 pts/0    00:00:00 ps -f

tab 2:

$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
jean      5363  5319  0 10:37 pts/1    00:00:00 bash
jean      5372  5363  0 10:37 pts/1    00:00:00 ps -f

$ ps -f -p5319
UID        PID  PPID  C STIME TTY          TIME CMD
jean      5319  2299  0 10:36 ?        00:00:02 /usr/lib/gnome-terminal/gnome-t
  • So it sounds like there's no way to "export" a variable to any other sibling or parent processes? – Xen2050 Apr 14 at 10:03
  • Correct. Alternatively you can set them in a script and then source this script in both tabs (. /path/to/script). – muclux Apr 14 at 10:11
  • Perfect answer, thank you! – Kirjain Apr 15 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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