The problem is you are using herestring and heredoc and wants to use IFS on the same way, while using expansion on an array.
The expansion works slightly different on every three.
Here Docs:
$ man bash | sed -n '/Here Doc/,/No/p'
Here Documents
This type of redirection instructs the shell to read input from the current source until a
line containing only delimiter (with no trailing blanks) is seen. All of the lines read up
to that point are then used as the standard input (or file descriptor n if n is specified)
for a command.
The format of here-documents is:
[n]<<[-]word
here-document
delimiter
No parameter and variable expansion, command substitution, arithmetic expansion, or path‐
Here Strings:
$ man bash | sed -n '/Command Substitution/,/Bash/p'
Command Substitution
Command substitution allows the output of a command to replace the command name. There are
two forms:
$(command)
or
`command`
Bash performs the expansion by executing command in a subshell environment and replacing
A working example:
#!/bin/bash
declare -a arr=(12 34 56 78 90)
# Setting IFS for the entire script
IFS=','
echo "Here String with variable expansion, IFS=',' is working:"
cat <<<"${arr[*]:0:3}"
echo "Here Document without variable expansion, IFS=',' is not working, because it is not inside cat <<EOF...EOF:"
cat <<EOF
${arr[*]:0:3}
EOF
echo "Echoing with command substitution in a subshell before using heredoc"
cat <<EOF
$(IFS=','; echo "${arr[*]:0:3}")
EOF
echo "Printig with command substitution in a subshell before using heredoc"
cat <<EOF
$(IFS=','; printf "%s" "${arr[*]:0:3}")
EOF
Output:
Here String with variable expansion, IFS=',' is working:
12,34,56
Here Document without variable expansion, IFS=',' is not working, because it is not inside cat <<EOF...EOF:
12 34 56
Echoing with command substitution in a subshell before using heredoc
12,34,56
Printig with command substitution in a subshell before using heredoc
12,34,56
Explanation:
The need to use command substitution (e.g. $(...)) in a heredoc block to take advantage of IFS for array separation is related to how Bash supports text expansion in heredoc blocks and command -Substitution interpreted.
By default, in a heredoc block, text expansion is performed in the same way as in other parts of a script, including using IFS. However, the interaction between IFS and array expansion in heredoc blocks can in some cases produce unexpected results if you change the separators.
The use of command substitution (e.g. $(IFS=','; echo "${arr[*]:0:3}")) allows explicit control of the delimiters within the substitution block and ensures that IFS is changed only for the command executed in the substitution. This is useful for achieving the desired separation without having to change the setting of IFS for the entire heredoc block or script.
In short, using command substitution in a heredoc block allows finer control of IFS and other environment variables without affecting the entire environment of the heredoc block. It is a way to ensure that IFS is only changed for the specific parts of the text within the heredoc block where it is needed.
Maybe this helps.