Python: pexpect does not execute `backtick` command
<p>I'm trying to run this command:</p>

<pre><code>foo=`ls /`
</code></pre>

<p>Works perfectly on bash but not if I execute it via pexpect:</p>

<pre><code>p = pexpect.spawn("foo=`ls /`").interact()

// Gives error command was not found or not executable: foo=`ls
</code></pre>

<p>What is the cause and how do I fix it? I've even tried escaping the ` but it seems its not working.</p>

<p>The command you are trying to execute requires <code>bash</code>. <code>pexpect</code> does not pass your command through <code>bash</code>, instead invoking your executable directly as if it were the shell.</p>

<p>From <a href="http://pexpect.sourceforge.net/pexpect.html#spawn" rel="nofollow">the docs</a>:</p>

<blockquote>
  <p>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:</p>
</blockquote>

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

<p>Although the documentation doesn't mention them, this would certainly also apply to backticks. So write your code to invoke <code>bash</code> explicitly:</p>

<pre><code>p = pexpect.spawn('/bin/bash -c "foo=`ls /`"').interact()
</code></pre>

python bash pexpect