Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from stackoverflow.com Sep 27 '10 at 14:13

This question came from our site for professional and enthusiast programmers.

    
I assume firefox is in the $PATH echoed by the first command? – Nifle Sep 25 '10 at 16:44
up vote 2 down vote accepted

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.

share|improve this answer
    
Right on! Missed it - thanks. – ttsiodras Sep 25 '10 at 16:35

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).

share|improve this answer
    
Spot on - thanks. – ttsiodras Sep 25 '10 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .