Skip to content

Instantly share code, notes, and snippets.

@for2ando
Last active November 29, 2024 05:53
Show Gist options
  • Save for2ando/65c7c243289a27a1e15eb50cc1834ed1 to your computer and use it in GitHub Desktop.
Save for2ando/65c7c243289a27a1e15eb50cc1834ed1 to your computer and use it in GitHub Desktop.
PATH manipulation functions' library
# PATH manipulation functions' library
# if $1 exists in $PATH then true
path_exists() {
echo ":$PATH:" | grep -q ":$1:"
}
# remove all occurence of $1 in $PATH.
remove_path_all() {
PATH="$(echo $PATH| sed '
s|:'"$1"':|:|g
s|^'"$1"':||
s|:'"$1"'$||
s|^'"$1"'$||
')"
}
# add $1 to the head of $PATH.
add_path_first() {
PATH="$1${PATH:+:$PATH}"
}
# add $1 to the tail of $PATH.
add_path_last() {
PATH="${PATH:+$PATH:}$1"
}
# add $1 next to the first occurence of $2 in $PATH.
add_path_next_to() {
PATH="$(echo $PATH| sed '
s|^'"$2"':|&'"$1"':|
t
s|:'"$2"':|&'"$1"':|
t
s|:'"$2"'$|&:'"$1"'|
t
s|^'"$2"'$|&:'"$1"'|
')"
}
# add $1 previous to the first occurence of $2 in $PATH.
add_path_previous_to() {
PATH="$(echo $PATH| sed '
s|^'"$2"':|'"$1"':&|
t
s|:'"$2"':|:'"$1"'&|
t
s|:'"$2"'$|:'"$1"'&|
t
s|^'"$2"'$|'"$1"':&|
')"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment