-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpath-add
More file actions
executable file
·55 lines (52 loc) · 1.04 KB
/
path-add
File metadata and controls
executable file
·55 lines (52 loc) · 1.04 KB
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
#!/bin/bash
set -euf -o pipefail
out () { printf %s\\n "$*" ; }
##
# Add path(s) to the PATH environment variable
#
# Usage: path-add [-i] [-q] paths...
#
# -i: Insert at the front, instead of the end
# -q: Don't print any error messages
#
# From http://pastie.org/591350
#
# Contact: Joel Parker Henderson (joel@joelparkerhenderson.com)
# License: GPL
# Updated: 2015-01-25
##
ap_prefix=0
ap_quiet=0
OPTIND=1
while getopts iq var
do
case "$var" in
i) ap_prefix=1 ;;
q) ap_quiet=1 ;;
esac
done
shift $(( $OPTIND - 1 ))
for p in "$@"
do
p=${p%"${p##*[!/]}"} ## remove trailing slashes
case $p in
""|.) continue ;;
esac
case :$PATH: in
*:$p:*) [ $ap_quiet -eq 0 ] && out "addpath: $p already in path" >&2
continue
;;
esac
if [ -d "$p" ]
then
if [ $ap_prefix -eq 1 ]
then
PATH="$p:$PATH"
else
PATH="$PATH:$p"
fi
else
[ $ap_quiet -eq 0 ] && out "addpath: $p is not a directory" >&2
fi
done
export PATH