Shell Functions and Path Variables, Part 3

A continuation of our introduction to path variables and elements.

In this final article in the series, I'll describe the remaining path handling functions and point out a few implementation issues. Before I do that, however, I will describe a utility called makepath. This reads either standard input or its argument list, builds a colon-separated path variable (pathvar) from those lines read and echoes it to standard output. For example:

$ makepath /bin /usr/bin /opt/kde/bin
makepath is used in several of the pathvar utilities to reconstruct a pathvar after its path elements (pathels) have been altered. I won't show you the innards of makepath, as they're somewhat tangential to the main topic and rather trivial.


First, let's look at listpath, which echoes the pathels making up a pathvar on separate lines, as in:

$ listpath -p MANPATH

Using listpath has two advantages over merely echoing $MANPATH. First, it's much easier to read the pathels when they appear on separate lines; and secondly, you can pipe its output through grep:

$ listpath | grep bin
There is no option-handling code we did not see in the addpath function, so let's look at the main code:
eval echo
This is very simple. We just echo the contents of the specified pathvar into the colon2line function (included in the tar file mentioned at the end of this article), which turns the embedded : characters into newline characters. I described the operation of this piece of code in some detail in Part 2, so I won't repeat it here. Take a look at that article ( if you're not sure why the eval is there.


We have already seen the addpath function, which performs an idempotent addition of a pathel to a pathvar. The converse of this behaviour is provided by delpath, which removes pathels from a pathvar. So, for example:

delpath /opt/CC/test/bin

will remove the /opt/CC/test/bin directory from $PATH and:

delpath -e "(bill|steve)" -p MANPATH
will remove all pathels matching the egrep-style regular expression "(bill|steve)" from $MANPATH. The command
delpath -n
removes all non-existent directories from $PATH.

Although delpath is not a function you are likely to need often, there is one place where it can be useful. Many UNIX machines have a file called /etc/PATH, which is sourced by /etc/profile. It sets up a default PATH containing directories required by all users. Too often, though, /etc/PATH is not modified for years at a time, and the directories added either no longer exist or are not truly required by all. In this case, you can call delpath at the start of an appropriate login script (.profile or .bash_profile) to remove the directories you do not need.

Let's look at the delpath code. I'll skip most of the option handling, as much of it is identical to that in addpath.

MATCH="-x"            # default
[ -n "$opt_e" ] && MATCH= # make grep use regexps
FILTER=              # default
[ -n "$opt_n" ] && FILTER="| realpath_filter"

Here, we see the final section of the option handling. The MATCH variable determines whether we handle the supplied path description as a regular expression or not. It is used later as an option to grep; grep -x tells grep to perform exact string matches.

The FILTER variable implements the -n option, i.e., the “remove non-existent directories” behaviour. If the user supplies the -n option, FILTER contains a string which pipes the output of a previous command through a program called realpath_filter. This program reads directory names from its standard input and writes the name to standard output only if it is an existing directory. I'll leave it as an easy exercise for the reader to implement such a filter.

The remainder of delpath is as follows:

eval listpath -p $pathvar $FILTER |
  grep -v -E $MATCH "$1"> /tmp/makepath_in.$$
eval $pathvar=$(makepath < /tmp/makepath_in.$$)
rm /tmp/makepath_in.$$

The function does its work in three stages. The first command generates a file in /tmp containing those directories that are not to be deleted. The second command rebuilds the pathvar from that file using makepath. Finally we remove the file; we don't want it cluttering up the file system after the function finishes. (The shell expands $$ to the process ID of the shell running the command; I'll assume it is 20610 in this article.)

Let's look at the first line. Essentially, it uses listpath to break the appropriate pathvar into separate lines, and grep to remove those we don't want. It's slightly complicated, however, by the presence of the FILTER variable. Suppose the user types:

delpath -e "^opt"

which means “remove all directories starting with the opt string from $PATH”. In this case, pathvar will contain PATH, while MATCH and FILTER will be empty. The first line will therefore expand to:

eval listpath -p PATH |
  grep -v -E "^opt<" > /tmp/makepath_in.20610
This is straightforward—listpath writes the pathels in PATH into the grep command, which we use to echo non-matching lines only (-v). We redirect the output into our temporary file, which will contain those pathels not starting with opt. In this case, the leading eval is unnecessary. However, if the user types
delpath -n
to remove all non-existent directories from $PATH, then the first line expands to
eval listpath -p PATH | realpath_filter |
grep -v -E "" > /tmp/makepath_in.20610
During the initial processing of the line (i.e., before the eval has forced re-evaluation), the shell saw the pipe symbol preceding the grep, but it did not see the pipe symbol preceding realpath_filter. As things stand at the moment, the shell sees the first | as a literal character and will pass it as an argument to listpath. This happened because the shell looks for | characters before it expands variables, and the | character preceding realpath_filter was stored in a variable. The second evaluation caused by the eval ensures the pipeline that runs the realpath_filter command is constructed.

We now have a file containing only the required pathels. The second line in delpath rebuilds the pathvar from that file, using the following code:

eval $pathvar=$(makepath < /tmp/makepath_in.$$)

This shouldn't cause us too many problems. First, makepath simply reads the lines in the file, builds a colon-separated pathvar and echoes it. We run makepath in command-substitution mode (that's the $(...) which I described in Part 2), so makepath's output is used as the right-hand side of the variable assignment. The initial eval is required due to the order in which the shell evaluates a command. Because it looks for assignment statements before expanding variables, it won't recognize that the command contains a valid assignment. The eval ensures the assignment takes place the second time the line is processed.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Source Code Availability - pathfunc.tgz

Rick Lantaigne's picture

Great article! Can the code be made available?? Email will do.

Where's the source code?

Anonymous's picture

The ftp link at the end is password protected. Shouldn't it be open to the public?