How to keep from duplicating path variable in csh

前端 未结 12 944
我在风中等你
我在风中等你 2020-11-27 05:19

It is typical to have something like this in your cshrc file for setting the path:

set path = ( . $otherpath $path )

but, the path gets dup

12条回答
  •  误落风尘
    2020-11-27 05:56

    When setting path (lowercase, the csh variable) rather than PATH (the environment variable) in csh, you can use set -f and set -l, which will only keep one occurrence of each list element (preferring to keep either the first or last, respectively).

    https://nature.berkeley.edu/~casterln/tcsh/Builtin_commands.html#set

    So something like this

    cat foo.csh # or .tcshrc or whatever: set -f path = (/bin /usr/bin . ) # initial value set -f path = ($path /mycode /hercode /usr/bin ) # add things, both new and duplicates

    Will not keep extending PATH with duplicates every time you source it:

    % source foo.csh % echo $PATH % /bin:/usr/bin:.:/mycode:/hercode % source foo.csh % echo $PATH % /bin:/usr/bin:.:/mycode:/hercode

    set -f there ensures that only the first occurrence of each PATH element is kept.

提交回复
热议问题