Here are compact bash
functions, which:
- avoid
eval
by using printf -v
for setting a variable indirectly, as in @Charles Duffy's excellent answer.
- use just parameter expansion to manipulate the path list.
- are self-contained (they do not rely on each other - at the expense of duplicating some code).
- force pre/appending, as in the OP's functions: i.e., if the specified path entry already exists but in a different position, it is forced into the desired position.
Caveat: Existing duplicate entries are only removed if they're not directly adjacent (see below for an alternative).
#!/usr/bin/env bash
# The functions below operate on PATH-like variables whose fields are separated
# with ':'.
# Note: The *name* of the PATH-style variable must be passed in as the 1st
# argument and that variable's value is modified *directly*.
# SYNOPSIS: path_prepend varName path
# Note: Forces path into the first position, if already present.
# Duplicates are removed too, unless they're directly adjacent.
# EXAMPLE: path_prepend PATH /usr/local/bin
path_prepend() {
local aux=":${!1}:"
aux=${aux//:$2:/:}; aux=${aux#:}; aux=${aux%:}
printf -v "$1" '%s' "${2}${aux:+:}${aux}"
}
# SYNOPSIS: path_append varName path
# Note: Forces path into the last position, if already present.
# Duplicates are removed too, unless they're directly adjacent.
# EXAMPLE: path_append PATH /usr/local/bin
path_append() {
local aux=":${!1}:"
aux=${aux//:$2:/:}; aux=${aux#:}; aux=${aux%:}
printf -v "$1" '%s' "${aux}${aux:+:}${2}"
}
# SYNOPSIS: path_remove varName path
# Note: Duplicates are removed too, unless they're directly adjacent.
# EXAMPLE: path_remove PATH /usr/local/bin
path_remove() {
local aux=":${!1}:"
aux=${aux//:$2:/:}; aux=${aux#:}; aux=${aux%:}
printf -v "$1" '%s' "$aux"
}
If you do need to deal with directly adjacent duplicates and/or want to be able to specify a different field separator, here are more elaborate functions that use the array technique from @konsolebox' helpful answer.
# SYNOPSIS: field_prepend varName fieldVal [sep]
# SEP defaults to ':'
# Note: Forces fieldVal into the first position, if already present.
# Duplicates are removed, too.
# EXAMPLE: field_prepend PATH /usr/local/bin
field_prepend() {
local varName=$1 fieldVal=$2 IFS=${3:-':'} auxArr
read -ra auxArr <<< "${!varName}"
for i in "${!auxArr[@]}"; do
[[ ${auxArr[i]} == "$fieldVal" ]] && unset auxArr[i]
done
auxArr=("$fieldVal" "${auxArr[@]}")
printf -v "$varName" '%s' "${auxArr[*]}"
}
# SYNOPSIS: field_append varName fieldVal [sep]
# SEP defaults to ':'
# Note: Forces fieldVal into the last position, if already present.
# Duplicates are removed, too.
# EXAMPLE: field_append PATH /usr/local/bin
field_append() {
local varName=$1 fieldVal=$2 IFS=${3:-':'} auxArr
read -ra auxArr <<< "${!varName}"
for i in "${!auxArr[@]}"; do
[[ ${auxArr[i]} == "$fieldVal" ]] && unset auxArr[i]
done
auxArr+=("$fieldVal")
printf -v "$varName" '%s' "${auxArr[*]}"
}
# SYNOPSIS: field_remove varName fieldVal [sep]
# SEP defaults to ':'
# Note: Duplicates are removed, too.
# EXAMPLE: field_remove PATH /usr/local/bin
field_remove() {
local varName=$1 fieldVal=$2 IFS=${3:-':'} auxArr
read -ra auxArr <<< "${!varName}"
for i in "${!auxArr[@]}"; do
[[ ${auxArr[i]} == "$fieldVal" ]] && unset auxArr[i]
done
printf -v "$varName" '%s' "${auxArr[*]}"
}