53

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.

Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51

1 Answers1

79

In csh, you can either try env:

% env LD_LIBRARY_PATH=/foo/bar myprogram

or, a subshell:

% (setenv LD_LIBRARY_PATH /foo/bar; myprogram)
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 3
    I got tripped up by the lack of an equal sign in the second version. Shows I need to read carefully ;) – drewish Apr 25 '13 at 17:51
  • 3
    The first one is using an external program (env) which is not tcsh, hence you'll loose your context, such as aliases. I would hence suggest rather using the second one. – orzel Dec 17 '17 at 01:40