Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Background, I am trying to use a Python library that spawns a subprocess using arguments ["bash", "-c", "python someScript.py"]. What's happening is Python 2.6 is executed, but I want python2.7 instead. Python 2.6 is in /usr/bin/ and Python 2.7 is in /usr/local/bin

If I say python -V and type python I get:

Python 2.7.10 and python is aliased to /usr/local/bin/python2.7

(Note, I have set up this alias in my .bashrc file: alias python=/usr/local/bin/python2.7)

However, if I invoke bash -c "type python" I get:

python is /usr/bin/python

Both echo $PATH and bash -c "echo $PATH" output the same path:

/usr/local/bin:/usr/local/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/lib:/usr/local/lib

Note, I tried to put /usr/local/bin first when I export PATH in my .bashrc.

So my question is, what is controlling the pathing (in this case to Python) when I execute a string command using bash -c ? Specifically, how do I get bash -c "python" to use Python 2.7? Edit: The arguments ["bash", "-c", "python someScript.py"] are hard-coded in the library, and I'd rather not have to modify the library source.

share|improve this question
up vote 0 down vote accepted

The problem is that while doing bash -c '....', you are spawning a non-interactive (and non-login) session of bash, which will not source any runtime configuration file e.g. ~/.bashrc.

As you have defined the alias in ~/.bashrc, you can use the -i option so that bash can simulate the interactive environment and hence source the ~/.bashrc file:

bash -ic '....'

Now the alias should be available.

Example:

$ alias l
alias l='ls'

$ bash -c 'l'
bash: l: command not found

$ bash -ic 'l'
bar  foo
share|improve this answer

Use the full path to the command. bash -c /usr/local/bin/python

share|improve this answer
    
Let me clarify that the subprocess arguments ["bash", "-c", "python someScript.py"] are hard-coded in the library. I'd like to find a solution that doesn't require modifying the library code. – RJM Nov 8 '15 at 21:57

I think your .bashrc is not sourced in a non-interactive shell such as this - try putting the PATH setting into your .profile file, which will also work for some other shells (or .bash_profile if you only want to set it for bash).

share|improve this answer

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.