Skip to content

jhunkeler/cleanpath

Repository files navigation

cleanpath

CMake

cleanpath is a utility that filters unwanted elements from an environment variable.

Installation

$ git clone https://github.com/jhunkeler/cleanpath
$ cd cleanpath
$ mkdir -p build
$ cd build
$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/.local
$ make install

Usage

usage: cleanpath [-hVDAelrsEv] [pattern ...]
  --help       -h    Displays this help message
  --version    -V    Displays the program's version
  --default    -D    Displays default operating system PATH 
  --list             Format output as a list
  --all        -A    Apply to all environment variables
  --all-list         Format --all output as a list
  --exact      -e    Filter when pattern is an exact match (default)
  --loose      -l    Filter when any part of the pattern matches
  --regex      -r    Filter matches with (Extended) Regular Expressions 
  --sep [str]  -s    Use custom path separator (default: ':')
  --env [str]  -E    Use custom environment variable (default: PATH)

Example

A typical MacOS path with Macports installed:

/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin

Remove MacPorts from the PATH

Exact match (default)

$ cleanpath /opt/local/bin /opt/local/sbin
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin

Loose match

$ cleanpath -l /opt/local
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin

Regex match

$ cleanpath -r ^/opt/local/.*
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin

Modifying other environment variables

$ TESTVAR=a:b/c:d/e:f
$ cleanpath -E TESTVAR -s / -l c
a:b/e:f

Using cleanpath in a script

#!/usr/bin/env bash
# Remove MacPorts and Fink
PATH=$(cleanpath -r '^/opt/local/.*' '^/opt/sw/.*')
export PATH

Using cleanpath to filter your entire runtime environment

#!/usr/bin/env bash
# Remove MacPorts and Fink from ALL environment variables
eval $(cleanpath -A -r '^/opt/local/.*' '^/opt/sw/.*')