On Tue, 01 Nov 2016 16:52:18 +1100, Steve D'Aprano wrote: > On Tue, 1 Nov 2016 04:00 pm, Wildman wrote: > >> 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 > > Bash is not involved here. Python is calling grep directly. > > > You don't have to believe us, you can test this yourself. Create a simple > text file with a single line containing your username, and a simple Python > script that calls grep as you have been: > > > [steve@ando ~]$ echo $USER > steve > [steve@ando ~]$ cat foo.txt > blah blah steve blah blah > [steve@ando ~]$ cat greptest.py > import subprocess > cmdlist = ['grep', '$USER', 'foo.txt'] > p = subprocess.Popen(cmdlist, stdout=subprocess.PIPE,stderr=subprocess.PIPE) > line, err = p.communicate() > print err, line > > [steve@ando ~]$ python2.7 greptest.py > > [steve@ando ~]$ > > > > So there you have it: categorical proof that bash does not expand the > string '$USER'. It cannot: bash is not involved in the subprocess call. > Python calls grep directly. > > If you want to expand the '$USER' string from Python, do it yourself:
I understand now. That explains more clearly why my original code did not work. Thank you. -- <Wildman> GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
