pure-bash-bible icon indicating copy to clipboard operation
pure-bash-bible copied to clipboard

Splitting a string on a delimiter

Open helpermethod opened this issue 3 years ago • 1 comments

A slightly nicer alternative to the proposed solution would be

split_string() {
   # Usage: split_string "string" "delimiter"
   printf '%s\n' "${1//$2/$'\n'}"
}

No need for creating an array first.

helpermethod avatar Jan 06 '22 12:01 helpermethod

This is very nice, but it's a terrible name as it interferes with /usr/bin/split, leading to hard-to-trace bugs. Please rename it to split_string or somesuch :-)

plijnzaad avatar Mar 17 '22 12:03 plijnzaad