Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am executing on a Ubuntu 12.04 shell the following command:

sudo bash -c "echo $PATH; which python"

which displays something like

/home/me/env/develop/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
/usr/bin/python

and the following command

ls /home/me/env/develop/bin

displys (amont others)

python

According to the man pages the which command looks for the given executable in all paths defined in PATH. The man page does not strictly specify the 'order' of the search, but usually it goes from the start to the end of the PATH environment variable.

Now, the path /home/me/env/develop/bin contains a python executable (modes set correctly, not a symbolic link), which is also the first path in PATH. But the which command displays a completly different location!

Why is that? I am doing something stupid? Has it magically to do with sudo? Is this a bug?

share|improve this question
    
use the bash builtin type instead of which –  glenn jackman Jul 11 '13 at 10:11

1 Answer 1

It's not a bug. $PATH within the double quotes gets expanded by your shell before bash -c even sees it.

This means, you'll actually run:

sudo bash -c "echo /home/me/env/develop/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games; which python"

Small test:

$ foo="hello"
$ sudo bash -c "echo $foo"
hello

To get to the root of your problem, run:

sudo bash -c 'echo $PATH; which python'

This won't expand $PATH before.

A solution for your path issue would be to set the path in your .bashrc instead of .bash_profile—that is if I'm assuming correctly your path is set there—so that it is loaded even for non-login shells (as in bash -c). Or you run bash -lc to have it run a login shell instead, which would load .bash_profile.

share|improve this answer
    
Thanks - that makes sense. –  Alex Jul 11 '13 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.