0

When I change an env var in /bin/sh, it only takes effect after a new export command, for example:

$ which ant
/usr/sfw/bin/ant
$ env | grep ANT
ANT_HOME=/usr/local/apache-ant-1.9.8
$ PATH=$ANT_HOME/bin:$PATH
$ which ant 
/usr/sfw/bin/ant
$ env | egrep '^PATH='
PATH=/usr/local/gcc-492/bin:/export/home/sisis/mpi-V50:...
$ export PATH
$ which ant
/usr/local/apache-ant-1.9.8/bin/ant
$ env | egrep '^PATH='
PATH=/usr/local/apache-ant-1.9.8/bin:/usr/local/gcc-492/bin:/export/home/sisis/mpi-V50 ...

Is this a bug or a new feature?

Matthias

Slipeer
  • 3,255
  • 2
  • 18
  • 32
guru
  • 1

1 Answers1

3

Feature. Not-exported variables are not visible to programs forked to. Export them to make them visible. PATH is inherited from the parent process, and will be passed on as-is to child processes such as env; only after being flagged with export will any changes in the shell process be visible to child processes. This can also be observed in the heirloom bourne shell:

$ PATH=/usr/bin:/bin
$ echo $PATH
/usr/bin:/bin
$ env | grep \^PATH
PATH=/Users/jdoe/bin:/Users/jdoe/usr/darwin15.0-x86_64/bin:/Users/jdoe/perl5/bin:...
$ export PATH
$ env | grep \^PATH
PATH=/usr/bin:/bin
$ PATH=/usr/local/bin:/usr/bin:/bin
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
$ env | grep \^PATH
PATH=/usr/local/bin:/usr/bin:/bin
$

Modern (or even other) shells differ; ZSH for example exports PATH by default:

$ PS1='%% ' zsh -f
% echo ${(t)PATH}
scalar-export-special
% 

However this different behavior has no influence on the SunOS sh or heirloom bourne sh.

thrig
  • 1,626
  • 9
  • 9
  • The var PATH is in the env already as my posting shows (line 8+9). – guru Feb 02 '17 at 15:31
  • @guru yes and that was inherited from the parent process. Only after `export` are local changes in the shell visible to child prcoesses. – thrig Feb 02 '17 at 16:36
  • I proved this on some other SunOS server and on FreeBSD: a change of the value of, for example, PATH is visible right away with `env | grep PATH` without an additional `export PATH` – guru Feb 02 '17 at 18:15
  • 1
    @gure that only proves that different shells are different. – thrig Feb 02 '17 at 18:23
  • ofc they are; but this "feature" or bug I see for the 1st time after 25 years of UNIX; btw: in Linux bash a change to `PATH` is also visible after this with `env` – guru Feb 02 '17 at 18:27