一只名叫tom的猫
Pascal Pilz用100%纯Bash重写解决方案(没有外部命令):function join_by { local IFS="$1"; shift; echo "$*"; }例如,join_by , a "b c" d #a,b c,djoin_by / var local tmp #var/local/tmpjoin_by , "${FOO[@]}" #a,b,c或者,我们可以使用printf来支持多字符分隔符,使用@gniourf_gniourf的思想。function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }例如,join_by , a b c #a,b,cjoin_by ' , ' a b c #a , b , cjoin_by ')|(' a b c #a)|(b)|(cjoin_by ' %s ' a b c #a %s b %s cjoin_by $'\n' a b c #a<newline>b<newline>cjoin_by - a b c #a-b-cjoin_by '\' a b c #a\b\c