Skip to content

Instantly share code, notes, and snippets.

@paulp
Created January 12, 2015 19:21
Show Gist options
  • Save paulp/4985c5dff0c6fe5d36c3 to your computer and use it in GitHub Desktop.
Save paulp/4985c5dff0c6fe5d36c3 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# e.g. mkString : a b c becomes a:b:c
# A single separator argument means the strings to join are on stdin.
#
# mkString "\n" < /some/file does get the newline
# through the obstacle course.
mkString () {
local sep="$1"
local first=1
while read line; do
if (( $first )); then
first=0
printf "%s" "$line"
else
printf "${sep}%s" "$line"
fi
done
echo ""
}
if [[ $# -gt 1 ]]; then
sep="$1" && shift && ( IFS="$sep" ; echo "$*"; )
else
mkString "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment