-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_functions
116 lines (105 loc) · 2.95 KB
/
env_functions
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# These are functions for manipulating PATH in shell config files such that the
# config file being loaded multiple times doesn't result in duplicate PATH
# entries.
# This allows using the Emacs package
# https://github.com/purcell/exec-path-from-shell to set environment variables
# for Emacs.
# Source: https://heptapod.host/flowblok/shell-startup/-/blob/65940865dc584083a16b23e458ed4945920af694/.shell/env_functions
indirect_expand () {
env |sed -n "s/^$1=//p"
}
# Usage: path_remove /path/to/bin [PATH]
# Eg, to remove ~/bin from $PATH
# path_remove ~/bin PATH
path_remove () {
local IFS=':'
local newpath
local dir
local var=${2:-PATH}
# Bash has ${!var}, but this is not portable.
for dir in `indirect_expand "$var"`; do
IFS=''
if [ "$dir" != "$1" ]; then
newpath=$newpath:$dir
fi
done
export $var=${newpath#:}
}
# Usage: path_prepend /path/to/bin [PATH]
# Eg, to prepend ~/bin to $PATH
# path_prepend ~/bin PATH
path_prepend () {
# if the path is already in the variable,
# remove it so we can move it to the front
path_remove "$1" "$2"
#[ -d "${1}" ] || return
local var="${2:-PATH}"
local value=`indirect_expand "$var"`
export ${var}="${1}${value:+:${value}}"
}
# Usage: path_append /path/to/bin [PATH]
# Eg, to append ~/bin to $PATH
# path_append ~/bin PATH
path_append () {
path_remove "${1}" "${2}"
#[ -d "${1}" ] || return
local var=${2:-PATH}
local value=`indirect_expand "$var"`
export $var="${value:+${value}:}${1}"
}
# Usage: source_if_exists filename
source_if_exists () {
if [ -r "$1" ]; then
. "$1"
fi
}
# Usage: indirect_expand PATH -> $PATH
indirect_expand () {
env |sed -n "s/^$1=//p"
}
# Usage: path_remove /path/to/bin [PATH]
# Eg, to remove ~/bin from $PATH
# path_remove ~/bin PATH
path_remove () {
local IFS=':'
local newpath
local dir
local var=${2:-PATH}
# Bash has ${!var}, but this is not portable.
for dir in `indirect_expand "$var"`; do
IFS=''
if [ "$dir" != "$1" ]; then
newpath=$newpath:$dir
fi
done
export $var=${newpath#:}
}
# Usage: path_prepend /path/to/bin [PATH]
# Eg, to prepend ~/bin to $PATH
# path_prepend ~/bin PATH
path_prepend () {
# if the path is already in the variable,
# remove it so we can move it to the front
path_remove "$1" "$2"
#[ -d "${1}" ] || return
local var="${2:-PATH}"
local value=`indirect_expand "$var"`
export ${var}="${1}${value:+:${value}}"
}
# Usage: path_append /path/to/bin [PATH]
# Eg, to append ~/bin to $PATH
# path_append ~/bin PATH
path_append () {
path_remove "${1}" "${2}"
#[ -d "${1}" ] || return
local var=${2:-PATH}
local value=`indirect_expand "$var"`
export $var="${value:+${value}:}${1}"
}
# Usage: source_if_exists filename
source_if_exists () {
# Test whether "$1" exists and is readable.
if [ -r "$1" ]; then
. "$1"
fi
}