sudo and PATH?

Derek Broughton news at
Mon Jan 8 19:46:18 UTC 2007

Andy wrote:

> On 08/01/07, Derek Broughton <news at> wrote:
>> derek at othello:~$ sudo echo $PATH
>> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games
>> OK, that's _my_ path (not root's).
> Doesn't the shell evaluate the $PATH variable prior to executing the
> command? thus it will show your path because at the time it does the
> substitution you are still you, and not root, yet. Its not until
> 'sudo' runs that you gain root privileges and by that time the
> expansion has already been completed.
>> derek at othello:~$ sudo -i
>> root at othello:~# echo $PATH
>> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
>> Those are the two paths set in login.defs, but the one you get directly
>> using sudo, rather than using sudo to get a shell, appears to be mine.
> This time you where root when the command was being expanded and thus
> it was expanded as roots path.

Thanks for the explanation.  The other one that always catches me is:

# sudo echo "foo" >>file-owned-by-root

for no-doubt-similar reasons.

More information about the ubuntu-users mailing list