How to set an environment variable for just one command in csh/tcsh

Tor Klingberg picture Tor Klingberg · May 10, 2011 · Viewed 35.4k times · Source

In bash, I can set a temporary environment variable for just one command like this:

LD_LIBRARY_PATH=/foo/bar myprogram

Can I do something similar in csh / tcsh? I could do

setenv LD_LIBRARY_PATH /foo/bar; myprogram; unsetenv LD_LIBRARY_PATH

, but that will lose any previous value the variable had.

Answer

dogbane picture dogbane · May 10, 2011

In csh, you can either try env:

% env LD_LIBRARY_PATH=/foo/bar myprogram

or, a subshell:

% (setenv LD_LIBRARY_PATH /foo/bar; myprogram)