As you know, bash shell itself is also process.

If I use command export a=c, then shell dynamically allocates variable a into location where environment variables are reside.(This environment section is described in process's memory descriptor) So, I think variable a is assigned into environment section. Also, it can be retrieved from command printenv

But, when I use command b=f, and then use echo $b, then variable expansion is operated. So the result is f. Where does this variable come from?

If I use command echo $a, then I can understand that the shell load this variable from section of environment variables.

But If I use command echo $b, where the shell load this variable? Is it heap section of process or somewhere?


All shell variables live in the same storage.

At startup, all environment variables are imported.

When a new command is launched, a new environment if created for this new command. All variables that are marked for export or that have been imported from the original environment are put into this new environment specfic to the new command.

  • I mean when I use k=kkk, the variable k is not shown in command printenv, I know when I use command, the new process starts. So new environment variables are exist for that process. But, when I just use k=kkk, it's not show in environment variables – A.Cho Feb 7 '16 at 14:01
  • This is explained by my answer: If you do not mark a new variable for export, it is not put into the environment for external commands to launch. It is only available inside the shell. – schily Feb 7 '16 at 18:03

If you assign a value that does not influence the environment of the running shell (I do not know whether that is possible at all).

The shell uses internal memory for all its variables (at least for the written ones). You can easily see that:

env - TESTVAR=foo bash

echo $TESTVAR 


echo $TESTVAR 

echo $$

cat /proc/13833/environ
  • But, in this case, your command env - TESTVAR=foo bash means "start new bash shell with environment variables which is shown on command(in this case TESTVAR), so TESTVAR must be shown environment variables. – A.Cho Feb 7 '16 at 14:03
  • @A.Cho TESTVAR is a shell environment variable but its value does not change as /proc tells you. That shows you that the shell does not use its environment variables to store variable values. – Hauke Laging Feb 7 '16 at 14:09
  • Now, I understand what you mean. But, in your example, What you said is true. But, printenv's TESTVAR(bar) and cat /proc/13833/environ's TESTVAR(foo) are different. What's the meaning of this difference then? – A.Cho Feb 7 '16 at 14:18
  • @A.Cho Subprocesses (like printenv) see the modified environment, not the original one. As schily said: The environment consists of all variables with the export flag. All variables from the shells environment have this flag. – Hauke Laging Feb 7 '16 at 14:38

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.