How to send a string with spaces from Python to Bash sub-process as a single value?
<p>I'm trying to send a variable from a python script to a bash script. I'm using <code>popen</code> like as shown below:</p>

<pre><code>subprocess.Popen(["bash", "-c", ". mainGui_functions.sh %d %s" % (commandNum.get(), entryVal)])
</code></pre>

<p>However, <code>entryVal</code> can sometimes contain one or more white space characters. In that case I divide the string into multiple arguments ($2,$3..)</p>

<p>How can i get it in one argument?</p>

<p>Simple solution #1: You do it the exact same way you'd do it if you were typing the input on the commandline; put it in quotes:</p>

<pre><code>subprocess.Popen(["bash", "-c", ". mainGui_functions.sh {} '{}'".format(commandNum.get(), entryVal)])
</code></pre>

<p>Simple solution #2: If <code>mainGui_functions.sh</code> is already executable, then you can just omit the <code>bash</code> part and pas args to it directly. In this case, <code>subprocess</code> takes care of making sure an entry with whitespace winds up as a single arg for you:</p>

<pre><code>subprocess.Popen(["mainGui_functions.sh", str(commandNum.get()), entryVal])
</code></pre>

python bash subprocess