Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Inaccurate Caveat for split() #118

Open
terminalforlife opened this issue Dec 24, 2021 · 0 comments
Open

Inaccurate Caveat for split() #118

terminalforlife opened this issue Dec 24, 2021 · 0 comments

Comments

@terminalforlife
Copy link

https://github.com/dylanaraps/pure-bash-bible#split-a-string-on-a-delimiter

This function works as intended in >= 3.1, unless I'm forgetting something. Prior to 3.1, you'll likely incorrectly see single-quotes in the output, if it even gets that far.

Tested on all full releases of BASH, from 3.0 to 5.1.8.

My approach would be:

split() {
    IFS=$1 read -a Buffer <<< "$2"
    printf '%s\n' "${Buffer[@]}"
}

It's even more portable, available in at least >= 3.0. The original is considerably convoluted, in my opinion.

One thing worth keeping in mind is that a here string in BASH returns an empty line if a null value is given to it, because of the newline character BASH adds after the fact, but given the nature of this function, I don't think that's a problem here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant