如何与heredoc一起进行进程替换

How to do process substitution together with heredoc

想做:

del_comments(){ sed 's/\s*#.*//;/^\s*$/d'; }

readarray -t arr < <( del_comments <<'EOF' )
a       # comm1
b       # comm2
# comm3
c
EOF    
printf "[%s]\n" "${arr[@]}"

它抱怨放置不当 'EOF'。上面的怎么写才正确?

想要 HEREDOCdel_comments 函数处理,结果应该转到 mapfile 以将行读入数组 arr.

需要输出(打印数组 arr)

[a]
[b]
[c]

正确的命令书写方式是:

readarray -t arr < <( del_comments <<EOF
a       # comm1
b       # comm2
# comm3
c
EOF
)

确保 EOF 单独在线上,没有任何前导或尾随白色space。

您可能需要将函数重写为:

del_comments() { sed 's/[  ]*#.*//; /^$/d'; }
  • [ ] 应该包含一个 space 和一个制表符。