Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created October 21, 2022 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weierophinney/ae887d4f9339aedaaa9f8fef4bd5798f to your computer and use it in GitHub Desktop.
Save weierophinney/ae887d4f9339aedaaa9f8fef4bd5798f to your computer and use it in GitHub Desktop.
Concatenate a list of strings using a comma in Bash
#########################################
## Concatenate a list of arguments with commas
##
## Outputs:
## Outputs all arguments as a single string, concatenated with commas
#########################################
concatenate_with_commas() {
local concatenated=""
local string
for string in "$@"; do
if [[ "${concatenated}" == "" ]]; then
concatenated+="${string}"
else
concatenated+=",${string}"
fi
done
echo "${concatenated}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment