How can I create an array from the first line of multiple text sources?

You wanted

lines+=("$line")

+=WORD is string concatenation (or addition). A compound assignment +=(...) appends the values to the array.

You probably also want to quote all your variable expansions here - the line definitely needs it if the line might contain whitespace, but you could have issues elsewhere as well.


In Bash you can also read lines into an array directly. If you select the current array length ${#lines[@]} as the insertion index, you can append to it:

for i in "${paths[@]}"; do
    mapfile -t -n 1 -O ${#lines[@]} lines < "$i"
done

Synopsis excerpt

mapfile mapfile [-n count] [-O origin] [-t] [array]

Read lines from the standard input into the indexed array variable array […]. Options, if supplied, have the following meanings:

  • -n: Copy at most count lines. If count is 0, all lines are copied.

  • -O: Begin assigning to array at index origin. The default index is 0.

  • -t: Remove a trailing newline from each line read.