On Tue, 01 Nov 2016 12:08:52 +1100, Ben Finney wrote: > Wildman via Python-list <python-list@python.org> writes: > >> On Mon, 31 Oct 2016 15:44:13 +1100, Ben Finney wrote: >> >> > One immediate difference I see is that you specify different >> > arguments to ‘grep’. You have a different pattern for each command. >> > >> > * The ‘^user\:’ pattern matches “user\:” at the start of a line. >> > >> > * The ‘^$USER\:’ pattern I think won't match anything, since “$” matches >> > end-of-line and then you expect further characters *past* the end of >> > the line. I think that will always fail to match any line. >> >> Yes, the '^' indicates the start of the line and the ':' indicates >> the character where to stop. The colon has a special meaning so it >> has to be escaped, '\:'. The dollar sign precedes a variable. In >> this case it is an environment variable. > > The ‘grep’ program you're invoking knows nothing of such variables, and > the ‘$’ sign means to ‘grep’ what I said above.
You are correct about that but, in this case grep never "sees" the '$' sign. Bash expands $USER to the actual user name beforehand. If you are on a Linux system, enter this into a terminal to illustrate: sudo grep ^$USER\: /etc/shadow If the user name is ben then grep would see this: grep ben\: /etc/shadow >> > Maybe you are expecting Bash to be involved somehow (and so “$USER” >> > will be substituted by Bash with some other value). That's not what >> > happens. >> >> No, the shell is already running. > > I don't know what you mean by this. If you mean that some *other* > instances of the shell ar running: that isn't relevant to how your > Python program invokes a subprocess. I simply meant that the script is run from a terminal. > The shell is not involved in the command as you invoke it directly as a > subprocess, without asking for a shell. > >> And $USER will be substituted by the name of the user that invoked the >> shell. > > It will not, because there is no shell involved: your Python program > invokes ‘sudo’, which invokes ‘grep’. The shell is never involved in > that chain, so its substitutions rules are irrelevant. I think my terminology is causing confusion. I apologize for that. -- <Wildman> GNU/Linux user #557453 -- https://mail.python.org/mailman/listinfo/python-list