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

I need to run a command under userx using sudo that is normally not available in the PATH. Command is available in bash under userx because of HOME/.bashrc.

Simply if I run in bash under userx

echo $PATH

I get the right PATH and command is available.

But if I run with sudo:

sudo -u userx echo $PATH

sudo -H -u userx echo $PATH

sudo su python -c 'echo $PATH'

sudo su python -c 'bash -c "echo $PATH"'

PATH variable is not set using userx's .bashrc.

How to run with sudo so that PATH will be normal PATH under userx?

share|improve this question

migrated from Jul 19 '12 at 2:08

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

up vote 2 down vote accepted

Run sudo with -i to have it start a new interactive shell (and load all the user configurations).

  -i [command]
               The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a
               login shell.  This means that login-specific resource files such as .profile or .login will be read by the shell.  If
               a command is specified, it is passed to the shell for execution via the shell's -c option.  If no command is
               specified, an interactive shell is executed.  sudo attempts to change to that user's home directory before running the
               shell.  The security policy shall initialize the environment to a minimal set of variables, similar to what is present
               when a user logs in.  The Command Environment section in the sudoers(5) manual documents how the -i option affects the
               environment in which a command is run when the sudoers policy is in use.
share|improve this answer
Oops, I read this in man, but did not realize this is what I need. Also it's possible to use su usrex -l -c "command" – user674618 Jul 18 '12 at 3:32
This needs the user's password instead of using sudo. – Benjamin Bannier Jul 18 '12 at 5:36

You must log in to answer this question.