How to send heredoc command to background?

I want to send a heredoc command like this one

cat <<EOF
line 1
line 2
EOF

to background. (The actual command is openssl with many lines of input which takes some time to complete).

The command needs to be storable in string so that I could exec it in PHP.

Simply appending an ampersand doesn’t work:

cat <<EOF
line 1
line 2
EOF &

Any other combinations I tried (putting the command in brackets etc.) don’t work either.

How?

Asked By: Greendrake

||

The here-doc operator (<<EOF, here) is just a regular redirection operator, and what comes after it (on the same line!) is still part of the command.

E.g.

# from here-doc to file
cat <<EOF > foo.txt
...
EOF

# here-doc and some arguments to cat
cat <<EOF -n foo.txt
...
EOF

# two here-docs!
cat /dev/fd/3 /dev/fd/4 3<<EOF 4<<EOF
first
EOF
second
EOF

So you’d do the same as if it was just a redirection from a file, put the & at the end of the cat command line:

cat <<EOF &
here-doc text
EOF
Answered By: ilkkachu