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