Search code examples
cshtcsh

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


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.


Solution

  • In csh, you can either try env:

    % env LD_LIBRARY_PATH=/foo/bar myprogram
    

    or, a subshell:

    % (setenv LD_LIBRARY_PATH /foo/bar; myprogram)