2

I have an array, and I would like to concatenate a subset of it, passing it along with some text as stdin for other commands to use.

This works in here string but not in here document, even the same code:

declare -a arr=(12 34 56 78 90)
IFS=','

# join a subset of arr by IFS in here string (OK)
cat <<<"${arr[*]:0:3}"

# doesn't work in here doc, all commas are gone. unless joining all elements
cat <<EOF
${arr[*]:0:3}
${arr[*]}
EOF

output:

12,34,56
12 34 56
12,34,56,78,90
philipxy
  • 14,867
  • 6
  • 39
  • 83
wuxunfeng666
  • 39
  • 1
  • 4
  • When I run your test script on my CentOS 7 system, it works perfectly the way you want it too. GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) – GoinOff Aug 16 '22 at 14:16
  • 3
    Works with 4.0.33; fails with 5.0.17. Possibly a bug. – jhnc Aug 16 '22 at 14:39

0 Answers0