Last active
December 10, 2015 22:18
-
-
Save benevans/4500526 to your computer and use it in GitHub Desktop.
Appends value(s) onto the given VAR with a delimiter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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