Skip to content

Instantly share code, notes, and snippets.

@benevans
Last active December 10, 2015 22:18
Show Gist options
  • Save benevans/4500526 to your computer and use it in GitHub Desktop.
Save benevans/4500526 to your computer and use it in GitHub Desktop.
Appends value(s) onto the given VAR with a delimiter.
#!/usr/bin/env bash
# Appends value(s) onto the given VAR, separated by delim.
# If not specified, delim is $IFS.
# Example: building a list of command-line options
# addto OPTIONS -server -Dfoo=true
# addto OPTIONS -host 127.0.0.1 -port 9999
# addto OPTIONS -debug
# program ${OPTIONS} "$@"
# usage: addto [-d delim] VAR [value*]
addto() {
local delim=${IFS}
if [ "$1" = "-d" ]; then
shift
delim="$1"
shift
fi
local v=$1; shift
while [ -n "$1" ]; do
eval "$v=\${$v:+\${$v}${delim}}\"$1\""
shift
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment