Giter VIP home page Giter VIP logo

bash-path's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.