6
$ unset foo
$ unset bar
$ echo $foo

$ echo $bar

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ foo=a
$ bar=b
$ export bar
$ echo $foo
a
$ echo $bar
b
$ PATH=
$ echo $PATH

$ /bin/bash
bash: lesspipe: No such file or directory
bash: dircolors: No such file or directory
bash: ls: No such file or directory
$ echo $foo

$ echo $bar
b
$ echo $PATH

$

As we can see, changing $PATH affects the subshell, whereas another variable needs to be exported. Why?

  • 4
    PATH is allready exported. So it affects the subshell. – Emmanuel Jul 22 '16 at 14:08
  • 1
    PATH is exported by default – cuonglm Jul 22 '16 at 14:08
  • 1
    Actually, changing foo affects subshells (future subshells that is, not already-existing ones). It doesn't affect child processes (some of which may happen to be a shell). See unix.stackexchange.com/questions/261638/… – Gilles Jul 23 '16 at 12:16
11

There are really two types of variable:

  1. Environment variables
  2. Shell variables

To make things more complicated, they both look the same, and a shell variable can be converted to an environment variable with the export command.

The env command will show the current set of environment variables.

$ myvar=100
$ env | grep myvar
$ export myvar
$ env | grep myvar
myvar=100

Variables can also be temporarily exported for the life of a command.

$ env | grep anothervar
$ anothervar=100 env | grep anothervar
anothervar=100
$ env | grep anothervar
$ 

When the shell starts up it inherits a number of environment variables (which may be zero).

Startup scripts (eg .bash_profile, .bashrc, files in the /etc directory) can also set and export variables.

Finally the shell, itself, may set a default number to environment variables if the environment is empty. e.g.

$ PATH=foo /bin/bash -c 'echo $PATH'
foo
$ PATH= /bin/bash -c 'echo $PATH'

$ unset PATH
$ /bin/bash -c 'echo $PATH'
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.
4

I recommend you read this article.

There is a big difference between shell and environment variables. PATH is one of environment ones, and is already exported (by default), so by changing it, the current shell your using and all the others - any child shells or processes - will be affected.

PATH is list of directories that the system will check when looking for commands. So even if you wanna change it or add something to it, it's better to use PATH="$PATH:... since you need to have the original PATH for executing commands, but if you alter this completely, you should use your commands by entering their absolute path, for instance /bin/pwd.

But foo which you're exporting is not something having any effect on shell or processes, I know you're trying to making it a environment variable, but you haven't defined something effective yet so that's why you cannot see what effect it can have on other things, in fact it's what you're defining because of you own needs at the moment, i.e. to keep track of some ephemeral data, so it'd be better to keep it just a shell variable. For instance, when you're trying to do some mathematical projects, you might be in need of defining something and have them for a short time, so you can set just some shell variables without needing to export them.

I hope I could be precise, but I still recommend you read the given article, and play around with changing some of those variables so you'll fully understand. BTW, if you want a environment variable to be permanent, you should add them as a line in your .bashrc or .bash_profile.

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.