aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: ef21cb7cc5860f65d6bc2602aa5c4833edeb4148 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# cleanpath

[![CMake](https://github.com/jhunkeler/cleanpath/actions/workflows/cmake.yml/badge.svg)](https://github.com/jhunkeler/cleanpath/actions/workflows/cmake.yml)

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


# Installation

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

# Usage

```text
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:
```shell
/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)
```shell
$ 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
```shell
$ cleanpath -l /opt/local
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin
```

### Regex match

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

## Modifying other environment variables

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

## Using cleanpath in a script

```shell
#!/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

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