title: How to set shell variables in subprocess.Popen (in a less ugly way...)
body: <p>NOTE: This is <em>not</em> the same question as <a href="https://stackoverflow.com/questions/1126116/python-persistent-shell-variables-in-subprocess">Python: Persistent shell variables in subprocess</a>, as that question is actually about environment variables, not shell variables.</p>
  
  <p>I'm trying to automate a basic benchmark that I'm doing in csh using the shell built-in <code>time</code> command.  You can tweak the output of <code>time</code> by setting the variable of the same name in the shell like follows:</p>
  
  <pre><code>set time=(1000000000 "%U %S %E %P %W %X %D %K %M %F %R %I %O %r %s %k %w %c")
  </code></pre>
  
  <p>However, this is of course not an environment variable, so you can't pass it in the <code>env</code> parameter in the <code>subprocess.Popen</code> constructor (I tried that first...).  You <em>can</em> hack it together by abusing (imo, anyhow) the <code>cmd</code> argument to do that, and then your actual work (shortened to display nicely):</p>
  
  <pre><code>time_cmd = 'set time=(1000000000 "%U %S %E %P %c"); time ./some_func'
  p = subprocess.Popen(cmd, executable="/bin/csh", shell=True, ...)
  </code></pre>
  
  <p>and that works as you would expect, but it seems not really the right way to go (and can lead to some pretty horrible <code>cmd</code> construction when you want to tweak a bunch of different shell behaviours).</p>
  
  <p>Is there any other way to set shell variables in <code>subprocess</code> other than this kludge?</p>