-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpandPath
More file actions
executable file
·37 lines (35 loc) · 807 Bytes
/
Copy pathexpandPath
File metadata and controls
executable file
·37 lines (35 loc) · 807 Bytes
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
#!/bin/bash
__track_usage "$(basename "$0")"
# Adapted from Charles Duffy: http://stackoverflow.com/a/29310477/1923878
expandPath() {
local path
local -a pathElements resultPathElements
IFS=':' read -r -a pathElements <<<"$1"
: "${pathElements[@]}"
for path in "${pathElements[@]}"; do
: "$path"
case $path in
"~+"/*)
path=$PWD/${path#"~+/"}
;;
"~-"/*)
path=$OLDPWD/${path#"~-/"}
;;
"~"/*)
path=$HOME/${path#"~/"}
;;
"~"*)
if [[ $path = */* ]]; then
path=${HOME}/${path#*/}
else
path=$HOME
fi
;;
esac
resultPathElements+=( "$path" )
done
local result
printf -v result '%s:' "${resultPathElements[@]}"
printf '%s\n' "${result%:}"
}
expandPath "$1"