How to keep from duplicating path variable in csh

前端 未结 12 878
我在风中等你
我在风中等你 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 06:12

    Using sed(1) to remove duplicates.

    $ PATH=$(echo $PATH | sed -e 's/$/:/;s/^/:/;s/:/::/g;:a;s#\(:[^:]\{1,\}:\)\(.*\)\1#\1\2#g;ta;s/::*/:/g;s/^://;s/:$//;')
    

    This will remove the duplicates after the first instance, which may or may not be what you want, e.g.:

    $ NEWPATH=/bin:/usr/bin:/bin:/usr/local/bin:/usr/local/bin:/bin
    $ echo $NEWPATH | sed -e 's/$/:/; s/^/:/; s/:/::/g; :a; s#\(:[^:]\{1,\}:\)\(.*\)\1#\1\2#g; t a; s/::*/:/g; s/^://; s/:$//;'
    /bin:/usr/bin:/usr/local/bin
    $
    

    Enjoy!

提交回复
热议问题