What happens to the environment when you run "su -c"?
Solution 1
What you are seeing is the fact that $PATH
is expanded in the first users shell during argument processing, before the su(1)
command runs, so it looks like it always does. If you use hard quotes ('echo $PATH'
) you should see something different, or just do \$
.
This will preserve the $PATH
syntax until after the su(1)
command runs. While it normally doesn't fiddle with the environment, it does start a new shell, and so you should check for PATH=
lines in the various shell startup scripts.
Your su(1)
has a -c
option, so you would seem to be on Linux. On a Mac or a BSD you would get a simplified PATH
instead of the login PATH
but you would still have the same "when did I expand PATH?" issue.
Solution 2
When su -
or su -l
is used, it emulates a login session, which involves resetting the environment to a clean state.
On Arch Linux, su -
uses the hardcoded string /usr/ucb:/bin:/usr/bin:/etc
as the new $PATH
. On other systems, it might read ENV_SUPATH
from /etc/login.defs
, or rely on PAM to set up environment.
su ... "echo $PATH"
lies, because the $PATH
part is expanded by your current shell, way before su
is launched. Use su ... 'echo $PATH'
instead (note the single quotes), or su - -c env
(prints the complete environment).
Related videos on Youtube
ttsiodras
Summary From 2002 to 2012, I was part-owner of a startup ( Semantix S.A. ), in the role of the company's Senior Software Engineer and Technical Lead. The company was acquired by Neuropublic S.A in June 2012. Tech I mostly code in Python and C/C++, targeting Linux, Windows and embedded development. In some of my projects I had to optimize for speed using CUDA and OpenMP/TBB. I can also code in x86/SSE asm if necessary. Scripting: Python mostly ; Perl in the past; daily one-liners with bash/awk/sed. SQL-wise: Oracle, MySQL and PostgreSQL (I've also written native apps over direct APIs: OCI for Oracle, psycopg2 for PostgreSQL). I begun my career 11 years ago, successfully coding Windows device drivers for 7 different FPGA designs that stress-tested Siemens 3G switches. I mostly use VIM these days but have no fear of IDEs (that's where I begun). That being said, I prefer Makefiles (recently, tup) to Eclipse-sized monsters. I value strong type systems and functional-style thinking (OCaml/F#). I am not an extremist in this regard, sometimes mutable state is the way to go (translation: I think Haskell takes it too far). I love ZFS - in general, data checksums in the filesystem. When they are not there, I use my own. I have a soft spot for Lisps. When my work in my startup demanded it, I skimmed over C#, Java and Windows administration. My programming/admin blog (Slashdotted, Reddit-ed, HN-ed, etc) Here ( mirrored on: http://ttsiodras.github.com/ ). My CV is here: https://www.thanassis.space/cv.pdf ( mirrored on: http://ttsiodras.github.io/cv.pdf )
Updated on September 17, 2022Comments
-
ttsiodras almost 2 years
What happens to the environment when you run "su -c"?
The reason I ask, is this mysterious behavior:
bash$ which firefox /usr/local/bin/firefox bash$ su - user -c "echo $PATH" bin:/usr/bin:/sbin:/usr/sbin:/opt/java/bin:/usr/local/bin:... bash$ su - user -c "firefox ..." -bash: firefox: command not found
Any ideas?
-
Julian almost 14 yearsI assume firefox is in the $PATH echoed by the first command?
-