4

According to the documentation for the subprocess module, its default shell is /bin/sh, but I have an ingrained, and probably irrational, aversion to hard-coding such constants.

Therefore, I much prefer to refer to some constant defined in subprocess. I have not been able to find any way to interrogate subprocess directly for this constant. The best I've managed is

def _getshpath():
    return subprocess.check_output('which sh', shell=True).strip()

or

def _getshpath():
    return subprocess.check_output('echo "$0"', shell=True).strip()

...both of which look pathetically fragile, since their validity ultimately depend on precisely the specific value I'm trying to determine in the first place. (I.e., if the value of this executable is not "/bin/sh", either definition could easily be nonsensical.)

What's best-practice for getting this path (without hard-coding it as "/bin/sh")?

Thanks!

4
  • subprocess itself hardcodes the path as /bin/sh. Maybe you should do the same or just use one of the methods you describe :) Commented Apr 27, 2012 at 22:51
  • Where is that path hard-coded (/bin/sh)? Is it a member of subprocess? Commented Apr 27, 2012 at 23:14
  • /bin/sh should be the standard POSIX shell, so you can't go wrong with that in a POSIX environment. In any other environment... who knows? Commented Apr 27, 2012 at 23:15
  • @ncRubert: no, it's just used as a string in the code. Commented Apr 29, 2012 at 1:26

2 Answers 2

3

Hard-coding it as /bin/sh is perfectly valid. If you look at the documentation for C's popen() you'll find it does this too. /bin/sh is, by construction, the system's standard shell.

Sign up to request clarification or add additional context in comments.

Comments

1

You could try

>>> import os
>>> shell = os.environ['SHELL']
>>> print shell
'/bin/bash'

You can use this to set the executable argument of subprocess.Popen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.