I can run setenv command just fine in my tcsh shell like this:

echo $0
echo $SHELL
setenv START_TIME `date +%s`

But, whenever I run it explicitly like shown below, it doesn't work. I need to do this because I have to specify shell when executing it via python subprocess call, otherwise it will call a sh shell as default.

/usr/bin/tcsh -i -f -c 'setenv START_TIME `date +%s`'
START_TIME: Undefined variable.

What's the problem here? I searched for this issue online, but couldn't find any issue similar to this. This is extremely frustrating.

  • 1
    A child process (such as your tcsh -c ...) cannot alter the environment of the parent process. If you need to change the environment of a particular process, do it in that particular process.
    – thrig
    Jul 6, 2017 at 16:49
  • @thrig, thanks! That was the issue, I'll try using set instead. Jul 6, 2017 at 16:55

1 Answer 1


You're running a separate invocation of the shell when you use /usr/bin/tcsh on the first line of your script. The echo command runs in the 'enclosing' shell - the one that has the commands in it.

Why not run both commands on the first line, like this:

/usr/bin/tcsh -i -f -c 'setenv START_TIME `date +%s`; echo $START_TIME'  

Then they are both running in the same invocation of the shell. Replace the echo command with a call to your python program if you wish.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .