pythonsshpexpectmetacharacters

pexpect and shell meta characters (>, |, or *)


I am pretty confused right now.

pexpect documentation states the following:

Remember that Pexpect does NOT interpret shell meta characters such as
redirect, pipe, or wild cards (>, |, or *). This is a common mistake.
If you want to run a command and pipe it through another command then
you must also start a shell. For example::

child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"')
child.expect(pexpect.EOF)

However I am looking at some old code that uses | and * in pexpect.sendline(some command | grep 'something'). So I started to test these command and they all seem to work. It is also worth mentioning that I am not using a modified pexpect module, it is plain old pexpect for python.

How come? why does pexpect mention that meta characters does not work, when it obviously does so?


Solution

  • pexpect.spawn doesn't interpret shell meta characters, but whatever's running inside pexpect (presumably a shell) clearly does:

    child = pexpect.spawn('/bin/bash')
    child.sendline('echo hello | cat')
    

    pexpect is just passing the string to the child process; it's not interpreting it.