Timothy R. Fesig wrote:
On Monday November 12 2012 00:06, Adam wrote:
I ran across a strange inconsistency today.  What should

      su -l -c 'echo $PATH'

return when run by an ordinary user (who happens to know the root
password) at the bash prompt?  Under Mandriva and CentOS it returns the
user's path, but under Debian it returns root's path.  I don't
understand the inconsistency or which response is correct, but that
would seem to make some shell scripts non-portable.
So far no one has mentioned this but there are a few reasons why there
might be problems with PATH not being what you would expect.

The following is for the bash shell. Process may be different for cshell or
zsh.

The only one I'm familiar with is the bash shell anyway, and that's what I was using on all the distros for the command above.

First is how shells are started. There are three types of shells:
   1. Login shell
   2. Normal shell
   3. Interactive shell

The login shell starts by running /etc/profile. This is where you would
normally find a setting for PATH. There may be basic settings here or
there may be tests for the userid and if root a special PATH would be
set.

On my Mandriva 2011.0 system, it looks like PATH is first set in /usr/bin/xdm (that's where the nonexistent /usr/ucb entry comes from) and added to, at both ends, in numerous files along the way. The only thing /etc/profile does to the PATH is append /usr/games for UID -ge 500 (ordinary users).

The normal and interactive shells would call the user's ~/.bashrc. This
file normally would invoke other files for proper setup.

Now for the command su -l -c 'echo $PATH'...

The single quotes around the echo would keep command line substution
of $PATH from occurring so the $PATH will not be resolved until the su
shell is running.

The command su is being run in the user's shell so the $PATH is
the user's PATH and unless changed that is what the echo would
display.

The argument -l requests that a login shell be used when switching
to the root userid. This would invoke the above mentioned files if
they are found.

Having the PATH show the user's settings sounds like the PATH was
not changed when the su command was run. This is not good.

So you're saying that command ought to return root's path (as with Debian), not the user's path (e.g. CentOS, Mandriva). That makes sense to me (essentially 'echo $PATH' is being executed as root), but other folks have said that Debian is wrong. I realize it's not a command likely to be used in real life, but I was surprised when different distros gave different results.

I hope this sheds some light on what might be happening.

Yes it does... thank you very much for your explanation!

Check all of
the above files and see if there is something wrong with where PATH
is being set. To see which files get control and in which order you
could place echos in the files.

Yes, that should be my next step, except it's hard to add 'echo' statements to a binary file like xdm. :-)

Adam

_______________________________________________
Mid-Hudson Valley Linux Users Group                  http://mhvlug.org
http://mhvlug.org/cgi-bin/mailman/listinfo/mhvlug

Upcoming Meetings (6pm - 8pm)                         Vassar College
 Dec 5 - SysAdmin Panel
 Jan 9 - High Performance Computing
 Feb 6 - February Meeting

Reply via email to