cajwine
cajwine

Reputation: 3160

How to do process substitution together with heredoc

Want to do:

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

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

it complaints about the badly placed 'EOF'. How to write the above correctly?

Want to have HEREDOC processed by the del_comments function and the result should go to mapfile for reading the lines into the array arr.

wanted output (from printing the array arr)

[a]
[b]
[c]

Upvotes: 1

Views: 184

Answers (1)

codeforester
codeforester

Reputation: 42999

The correct way to write your command is:

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

Make sure EOF is on the line by itself, without any leading or trailing whitespace.

Probably, you need to rewrite your function as:

del_comments() { sed 's/[  ]*#.*//; /^$/d'; }
  • [ ] should contain a space and a tab.

Upvotes: 4

Related Questions