Ubuntu – Do terminal tabs correspond to different shells

bashcommand lineenvironment-variables

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!

Best Answer

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