My file.txt contains the following content:-


1 Answer 1


If your ksh is ksh93 or mksh, you could read the first line into an array with:

IFS=, read -rA array < file.txt

Also works in zsh or yash. For some reason, bash, years later, chose to use -a instead of -A for that.

ksh93 can also do CSV parsing with -S (handling quotes and values spanning several lines):

IFS=, read -rSA array < file.txt

For instance, with a file.txt containing:

"foo  bar",,"blah""blah

That gives:

$ typeset -p array
typeset -a array=('foo  bar' '' $'blah"blah\nblah')

To split the whole file on commas, you can use split+glob (done implicitly upon unquoted $(<file)) with glob disabled:

set -o noglob
array=( $(<file.txt) )

Or for compatibility with ksh88 / pdksh:

set -o noglob
set -A array -- $(<file.txt)

Beware $(<...) also strips trailing newline characters.

Also note that if the file ends in a , (and that also applies with read above even with -S, except with zsh) that doesn't create an extra trailing empty element (, is treated as delimiter, not separator, despite what the S in $IFS might suggest).

  • Thank you, for sharing this! Jun 28, 2022 at 18:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .