2 Answers
2

To do what I think you are trying to do, which is read every comma separated field in true_pairscore.txt into an array variable there is not much wrong with your code. The $( at the start of the IFS line is obviously wrong and would cause the script to have an error (maybe this is the reason you get no output). Other than that, the main fix for your code would be just to remove the quotes around $line since you want bash to do word splitting around IFS (usually this is what you use them to avoid):

Update

If the embed.txt file is to be sourced in another bash shell, you probably want to quote each element of the written hero_pairscore array. To do this, replace the echo "hero_pairscore=( ${hero_pairscore[@]} )" with:

Because $* has the special POSIX-specified quality of splitting the shell's positional parameter argument array on the first character of $IFS you can easily convert swathes of data as simply as that. You can "quote"-protect the argument array and still split it on any single character you like. As long as you get it right the first time, that is.

APPEND/PREPEND

POSIX also specifies special qualities to "$@". Aside from the "$1"... eval "\${$#}" addressing forms for each argument, the "$@" parameter - when quoted - safely expands to all of the shell's arguments as they were last set regardless of the current value of $IFS. So if you want to add a list to the beginning of your array you do:

set -- $list "$@"

To the head/tail:

set -- $head "$@" $tail

To triple your array:

set -- "$@" "$@" "$@"

ALTOGETHER NOW

Unless your datastream is very large, the following might be a little quicker than your current operation (though, if you're working with files, you'd be far better off to eschew shell splitting altogether):