Let's look at what actually happens here by replacing tsort
with cat
:
$ arr1=( 1 2 2 3 )
$ arr2=( 2 3 3 4 )
$ cat <<END
> "${arr1[@]}" "${arr2[@]}"
> END
"1 2 2 3" "2 3 3 4"
As you can see, the here-document is nothing but a text string with the values of the arrays expanded inside it. The double quotes come from the double quotes in the document itself (the shell only cares about the ${...}
bit and won't touch the quote characters).
The output when deleting the double quotes would be the same, but without the double quotes,
1 2 2 3 2 3 3 4
This would be interpreted by tsort
as the pairs
1 2 <-- first two numbers from arr1
2 3 <-- last two numbers from arr1
2 3 <-- first two numbers from arr2
3 4 <-- last two numbers from arr2
It's unfortunate that you chose this particular example, as this happens to be exactly the same as
1 2 <-- first number from arr1 and arr2
2 3 <-- second number from arr1 and arr2
2 3 <-- etc.
3 4
i.e., the entries of each array running down in two columns (one column per array).
To generate this second list (correctly), you can't really use a here-document. Instead you could use a shell loop:
for (( i=0; i<${#arr1[@]}; ++i)); do
printf '%d %d\n' "${arr1[i]}" "${arr2[i]}"
done | tsort
wc
receives a single stream and counts newlines in it. – Kamil Maciorowski Nov 18 '18 at 2:52