Skip to content

Instantly share code, notes, and snippets.

@marcelomgarcia
Created July 4, 2019 14:23
Show Gist options
  • Save marcelomgarcia/ba29da78f9530ac065af1bce9e86ddcf to your computer and use it in GitHub Desktop.
Save marcelomgarcia/ba29da78f9530ac065af1bce9e86ddcf to your computer and use it in GitHub Desktop.
bash split a string "a, b, c" into an array arr[a, b, c]
#!/usr/bin/env bash
# Split a string passed as parameter into array elements.
# "a, b, c" -> arr[a,b,c]
# https://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash
function splity {
IFS=', ' read -r -a array <<< "$1"
}
# Before splitting the string:
splity "$1"
for element in "${array[@]}"
do
echo "$element"
done
echo "have a nice day."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment