Receive a shell command as an OptionParser string argument
<p>I am using OptionParser(), and define the following:</p>

<pre><code>parser.add_option("--cmd", dest="command", help="command to run")
</code></pre>

<p>However, if i provide a complex shell command, such as :</p>

<pre><code>python shell.py --cmd "for i in `seq 1 10`; do xxx; done"
</code></pre>

<p>And internally print <code>options.command</code>, i get something unexpected to me:</p>

<pre><code>for i in 1
2
3
4
5
6
7
8
9
10; do
</code></pre>

<p>Is there a good way to pass an OptionParser option which is a shell command?</p>

<p>When invoking:</p>

<pre><code>python shell.py --cmd "for i in `seq 1 10`; do xxx; done"
</code></pre>

<p>The shell first substitute the command enclosed in ` with its output. Thus, the command you actually invoke is:</p>

<pre><code>python shell.py --cmd "for i in 1
2
3
4
5
6
7
8
9
10; do ..."
</code></pre>

<hr>

<p>To avoid this:</p>

<p>Escape the ` character when invoking the command:</p>

<pre><code>python shell.py --cmd "for i in \`seq 1 10\`; do xxx; done"
</code></pre>

<p>Use strong quoting (string enclosed in ')</p>

<pre><code>python shell.py --cmd 'for i in `seq 1 10`; do xxx; done'
</code></pre>

python shell optionparser