Call a shell command from Python

Ben Finney ben+python at benfinney.id.au
Tue Nov 1 01:23:08 EDT 2016


Wildman via Python-list <python-list at python.org> writes:

> […] in this case grep never "sees" the '$' sign. Bash expands $USER to
> the actual user name beforehand.

I understand how Bash substitutes variables on the command line.

What I need to repeat, though: In this case, no, Bash doesn't do that
because Bash isn't getting involved. Grep does in fact see the “$” in
the command argument, because nothing ever replaces it.

> >> No, the shell is already running.
> > 
> > I don't know what you mean by this.
>
> I simply meant that the script is run from a terminal.

Which means that shell isn't involved in that command you're invoking
from inside the script.

The Bash instance which invoked your script is now *doing nothing*, and
will never do anything again until control returns to it (by your script
exiting, or suspending, or some other control interruption).

Your script invoking further programs will *not* get the earlier Bash
involved in that process (except in the special cases where those
programs themselves manipulate running processes; ‘sudo’ and ‘grep’ are
not special in this way).

So the way your script was invoked has no bearing on whether Bash will
get involved in what your script does. Your script is *directly*
invoking programs, and if you don't ask for a shell to be involved you
won't get it.

-- 
 \          “… a Microsoft Certified System Engineer is to information |
  `\     technology as a McDonalds Certified Food Specialist is to the |
_o__)                               culinary arts.” —Michael Bacarella |
Ben Finney




More information about the Python-list mailing list