1

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?

1 Answer 1

2

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
2
  • 1
    👍🏽 You can even do things like cmd1 << EOF | cmd2 3<< EOF2 & Jun 17 at 9:19
  • Or if grep -q word <<EOF; then with the here-doc data next, and the rest of the compound command coming after it.
    – ilkkachu
    Jun 17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.