Skip to content

Instantly share code, notes, and snippets.

@ntrrgc
Created October 14, 2022 17:03
Show Gist options
  • Save ntrrgc/0810b74bb10231968c91ad7d00926d4d to your computer and use it in GitHub Desktop.
Save ntrrgc/0810b74bb10231968c91ad7d00926d4d to your computer and use it in GitHub Desktop.
Function to add paths to env vars such as PATH or LD_LIBRARY_PATH without cluttering them
register_path() {
# Usage:
# register_path <env_var_name> <provided_path> [after]
#
# Registers a path in a PATH-like environment variable.
# The provided_path is prepended unless the "after"
# argument is provided, in which case it's appended.
#
# Example:
# register_path LD_LIBRARY_PATH /an/important/path
# register_path LD_LIBRARY_PATH /a/less/important/path after
#
local separator=":"
local env_var_name="$1"
local provided_path="$2"
local mode="prepend"
if [[ $# -ge 3 ]]; then
if [[ "$3" == "after" ]]; then
mode="append"
else
echo "register_path: WARNING: Unexpected arguments: $@"
fi
fi
# If the variable is not defined at this point,
# or it's blank, just set it to the provided path.
if [[ "${!env_var_name:-}" == "" ]]; then
export "$env_var_name=$provided_path"
return
fi
local wrapped_paths="$separator${!env_var_name}$separator"
# Remove any existing instances of provided_path within the path list
wrapped_paths="${wrapped_paths//$separator$provided_path$separator/$separator}"
# Add the wanted path on the desired side of the list
if [[ "$mode" == "prepend" ]]; then
wrapped_paths=":$provided_path$wrapped_paths"
else
wrapped_paths="$wrapped_paths$provided_path:"
fi
# Remove the wrapping separators
new_paths="${wrapped_paths#$separator}"
new_paths="${new_paths%$separator}"
# Export the clean version
export "$env_var_name=$new_paths"
}
# To run the unit test, this script should be executed (not sourced) and receive
# the --unit-test argument.
if ! (return 0 2>/dev/null) && [[ $# -gt 0 ]] && [[ $1 == "--unit-test" ]]; then
assert_var() {
local env_var="$1"
local expected="$2"
local actual="${!env_var}"
if [[ "$actual" != "$expected" ]]; then
echo "Test failed: $test_name"
echo " \$$env_var expected: $expected"
echo " \$$env_var actual: $actual"
exit 1
fi
}
test_name="Registering the first path for a variable"
register_path TEST_VAR "/test1/bin"
assert_var TEST_VAR "/test1/bin"
test_name="Appending to the beginning of a variable"
register_path TEST_VAR "/test2/bin"
assert_var TEST_VAR "/test2/bin:/test1/bin"
test_name="Appending to the end of a variable"
register_path TEST_VAR "/test3/bin" after
assert_var TEST_VAR "/test2/bin:/test1/bin:/test3/bin"
echo "Tests succeeded."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment