Skip to content

Instantly share code, notes, and snippets.

@boo1ean
Last active July 14, 2020 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boo1ean/5618604 to your computer and use it in GitHub Desktop.
Save boo1ean/5618604 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Split string into array
# SPLITTED_ARRAY is a global array to store split result
#
# Usage example:
#
# split "1,2,3", ","
# result=0
# for i in ${SPLITTED_ARRAY[*]}
# do
# result=$(($result + $i))
# done
#
# Will output "6"
declare -a SPLITTED_ARRAY
function split {
local OLDIFS=$IFS
IFS=$2
SPLITTED_ARRAY=()
for i in $1
do
SPLITTED_ARRAY+=($i)
done
IFS=$OLDIFS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment