Globbing/pathname expansion with colon as separator
This should do it for you:
dirs=(/var/lib/gems/*/bin) # put filenames (dirnames) in an array
saveIFS=$IFS IFS=':' # set the Internal Field Separator to the desired delimiter
dirs=("${dirs[*]}") # convert the array to a scalar with the new delimiter
IFS=$saveIFS # restore IFS
Actually, I thought of a better solution: use a shell function.
function join() {
local IFS=$1
shift
echo "$*"
}
mystring=$(join ':' /var/lib/gems/*/bin)
PATH="$(printf "%s:" /usr/*/bin)"
PATH="${PATH%:}"
No need to mess with IFS
, zsh can join arrays with a simple variable flag:
dirs=(/var/lib/gems/*/bin(N))
dirs=${(j.:.)dirs}
The (N)
on the first line suppresses a warning if there are no files; the (j.:.)
joins the array with :
s. Works with 0, 1, or multiple matches.