How to split a string on a deliminator in zsh
1 min read

How to split a string on a deliminator in zsh

How to split a string into an array in zsh

As zsh has no concept of readarray which makes reading and splitting a string as an array in zsh more difficult than in my opinion it needs to be, here's an example of how to acheive it in zsh:


Say we have a string one-two-three and we want to split that string to read one two three

First set the input to a variable:

INPUT=one-two-three

Now we have a string we can create an array from, splitting on each -

ARRAY=("${(@s/-/)INPUT}")

Now we can access each part of the array like so:

echo ${ARRAY[1]}
echo ${ARRAY[2]}
echo ${ARRAY[3]}

output:

one
two
three