Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep from duplicating path variable in csh

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

set path = ( . $otherpath $path )

but, the path gets duplicated when you source your cshrc file multiple times, how do you prevent the duplication?

EDIT: This is one unclean way of doing it:

set localpaths = ( . $otherpaths )
echo ${path} | egrep -i "$localpaths" >& /dev/null
if ($status != 0) then
    set path = ( . $otherpaths $path )
endif
like image 461
dr_pepper Avatar asked Sep 25 '08 20:09

dr_pepper


People also ask

What is stored in the $PATH variable?

The PATH variable is an environment variable containing an ordered list of paths that Linux will search for executables when running a command. Using these paths means that we don't have to specify an absolute path when running a command.

How do I fix the PATH variable on a Mac?

If the PATH variable keeps resetting on your Mac, it could be because it isn't permanently set. And so, you must edit your system's default shell config file and add the default paths along with the path for the program/script you intend you want to be accessible globally to it.

Where are PATH variables stored mac?

The default PATH and MANPATH values are in /etc/paths and /etc/manpaths . And also the path-helper reads files in the etc/paths. d and /etc/manpaths. d directories.


1 Answers

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.

like image 181
Dotan Dimet Avatar answered Oct 09 '22 04:10

Dotan Dimet