Functions to modify colon separated variables like `$PATH` or `$MANPATH`
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

Bash Path

Functions to modify colon separated variables like $PATH or $MANPATH

Installation

bics

Use bics to manage this plugin

After installing bics, install this plugin by running

bics install git://github.com/bahamas10/bash-path.git

manually

git clone git://github.com/bahamas10/bash-path.git
cd bash-path
cat path.bash >> ~/.bashrc
exec bash

Example

Given the following environment:

$ . path.bash
$ PATH='/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'

You can add to $PATH using path_add (similar to pathmunge):

$ path_add /my/new/bin
$ path_add /my/new/sbin before
$ echo "$PATH"
/my/new/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/my/new/bin

Duplicates are also allowed (path_add does what you tell it to do):

$ path_add /bin
$ path_add /bin
$ path_add /bin
$ echo "$PATH"
/my/new/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/my/new/bin:/bin:/bin:/bin

You can remove from $PATH using path_remove:

$ path_remove /usr/local/bin
$ echo "$PATH"
/my/new/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/my/new/bin:/bin:/bin:/bin

Removing a path that is not present is a noop:

$ path_remove /usr/local/bin
$ echo "$PATH"
/my/new/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/my/new/bin:/bin:/bin:/bin

Duplicates are also completely removed with path_remove:

$ path_remove /bin
$ echo "$PATH"
/my/new/sbin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/my/new/bin

Let's add more garbage to the $PATH:

$ path_add /bin
$ path_add /bin
$ path_add /bin
$ path_add /bin
$ path_add /bin/
$ path_add /bin////
$ path_add /some-fake-dir
$ echo "$PATH"
/my/new/sbin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/my/new/bin:/bin:/bin:/bin:/bin:/bin/:/bin////:/some-fake-dir

Finally, path_clean can be used to "cleanup" the path:

$ path_clean
$ echo "$PATH"
/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/bin

Usage

path_add

path_add <path> [direction] [varname]

path_add takes a directory name, an optional direction name (defaults to "after" to append to list) and an optional variable name (defaults to PATH) and adds the directory name to data stored in variable.

PATH='/bin:/sbin'

path_add /usr/bin
# PATH => '/bin:/sbin:/usr/bin'

path_add /opt/local/bin before
# PATH => '/opt/local/bin:/bin:/sbin:/usr/bin'

# The variable name can also be passed by name.
foo=''
path_add /bin after foo
# foo => '/bin'

path_remove

path_remove <path> [varname]

path_remove takes a directory name and an optional variable name (defaults to PATH) and removes every instance of the directory name from the data stored in the variable.

PATH='/bin:/sbin:/usr/bin:/usr/sbin'

path_remove /usr/bin
# PATH => '/bin:/sbin:/usr/sbin'

path_remove /not-found
# PATH => '/bin:/sbin:/usr/sbin'

# The variable name should be passed by name.
foo='/bin:/sbin'
path_remove /bin foo
# foo => '/sbin'

path_clean

path_clean [varname]

path_clean takes an optional variable name (defaults to PATH) and "cleans" it, this process will:

  1. Remove empty elements.
  2. Remove relative directories.
  3. Remove directories that don't exist/can't be accessed (checked with cd).
  4. Remove duplicates (first element stays, subsequent elements are tossed).
PATH='::/bin:/sbin:::./:../../some-path::/doesnt-exist'
path_clean
# PATH => '/bin:/sbin'

PATH='/bin:/bin//:////bin//////:/bin/dir/..::'
path_clean
# PATH => '/bin'

Each function has a print derivative that will echo the result instead of setting it to the variable given.

path_print_add

Same usage as path_add

path_print_remove

Same usage as path_remove

path_print_clean

Same usage as path_clean

Exports

Global Variables

None

Environmental Variables

None

Functions

  • path_add() - add to path, similar usage to pathmunge
  • path_remove() - remove from path
  • path_clean() - clean path
  • path_print_add() - add to path and print result without modification
  • path_print_remove() - remove from path and print result without modification
  • path_print_clean() - clean path and print result without modification

Aliases

None

Testing

Run make test to run the test suite:

$ make test
./test
# path_add bad args
# path_remove bad args
# path_print_add bad args
# path_print_remove bad args
# simple path_add after
# simple path_add before
# path_add
# path_remove
# path_add bad directories
# path_clean simple
# path_clean complex
# path_print_remove
# path_print_add
# path_print_clean

Syntax can also be checked (requires shellcheck and awk) with make check:

$ make check
awk 'length($0) > 80 { exit(1); }' < Makefile
awk 'length($0) > 80 { exit(1); }' < path.bash
awk 'length($0) > 80 { exit(1); }' < test
shellcheck path.bash
shellcheck -e SC1091,SC2034 test

License

MIT License