

# zsh join.sh
# or
# bash join.sh

strings="go
python
rust
js
"

printf "\n# join by tr:\n"
echo "$strings" | tr -s "\n" "-"

printf "\n# join by paste:\n"
echo "$strings" | paste -s -d"-" -

printf "\n# join by awk printf:\n"
echo "$strings" | awk '{printf("%s%s", sep, $0); sep="-"} END {print}'

printf "\n#join by awk line-concat:\n"
echo "$strings" | awk '{s=(s d)$0; d=", "  } END {print s}'

printf "\n# join by /usr/bin/printf:\n"
join_by_printf() {
	aaa=(`echo $strings`)
	if [ -n "$ZSH_VERSION" ]; then
		# assume Zsh
		echo "zsh: $ZSH_VERSION"
		# 1. use echo
		# aaa=(`echo $strings`)
		# 2. use <<<
		# aaa=(`<<< $strings`)
		printf "%s, " "${aaa[@]}" # '@' not '*'

	elif [ -n "$BASH_VERSION" ]; then
		# assume Bash
		echo "bash: $BASH_VERSION"
		# aaa=(`echo $strings`)
		printf "%s, " "${aaa[@]}" # '@' not '*'

	else
		# assume something else
		echo "$string"
	fi
	echo
	echo "use 'printf -v' to keep result and '\${VARIABLE%SEPERATOR}' to trim the ending ', ' "
	printf -v KEEP "%s, " "${aaa[@]}"; echo "${KEEP%, }"
}
join_by_printf


printf "\n# join by echo ARRAY:\n"
(
	aaa=(`echo $strings`)
	IFS=","; echo "${aaa[*]}"
)
