Output from subprocess.Popen()

Clodoaldo Pinto Neto clodoaldo.pinto at gmail.com
Mon Oct 16 20:28:11 EDT 2006


Fredrik Lundh wrote:
> sjdevnull at yahoo.com wrote:
>
> > I can't see any obvious way to ask subprocess to use a shell other than
> > the default.
>
> -c ?
>
>  >>> f = Popen(["/bin/bash", "-c", "set|grep IFS"], stdout=PIPE)
>  >>> f.stdout.read()
> "IFS=$' \\t\\n'\n"
>  >>> f = Popen(["/bin/sh", "-c", "set|grep IFS"], stdout=PIPE)
>  >>> f.stdout.read()
> "IFS=' \t\n"

It solves my problem:
>>> f = sub.Popen(['/bin/sh', '-c', 'set|grep IFS'], stdout=sub.PIPE)
>>> f.stdout.read()
"BASH_EXECUTION_STRING='set|grep IFS'\nIFS=' \t\n"
>>> f = sub.Popen(['/bin/bash', '-c', 'set|grep IFS'], stdout=sub.PIPE)
>>> f.stdout.read()
"BASH_EXECUTION_STRING='set|grep IFS'\nIFS=$' \\t\\n'\n"

But I still don't understand what is happening. The manual says that
when shell=True the executable argument specifies which shell to use:

>>> f = sub.Popen('set|grep IFS', shell=True, executable='/bin/sh', stdout=sub.PIPE)
>>> f.stdout.read()
"BASH_EXECUTION_STRING='set|grep IFS'\nIFS=' \t\n"
>>> f = sub.Popen('set|grep IFS', shell=True, executable='/bin/bash', stdout=sub.PIPE)
>>> f.stdout.read()
"BASH_EXECUTION_STRING='set|grep IFS'\nIFS=' \t\n"

To make my confusion bigger, in Fedora sh is just a link to bash:
$ ll /bin/*sh
-rwxr-xr-x 1 root root  720888 Feb 11  2006 /bin/bash
lrwxrwxrwx 1 root root       4 Aug 28 22:53 /bin/csh -> tcsh
-rwxr-xr-x 1 root root 1175496 Aug 17 13:19 /bin/ksh
lrwxrwxrwx 1 root root       4 Feb 24  2006 /bin/sh -> bash
-rwxr-xr-x 1 root root  349312 Aug 17 15:20 /bin/tcsh
-rwxr-xr-x 1 root root  514668 Feb 12  2006 /bin/zsh




More information about the Python-list mailing list