4

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

5

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 & Commented Jun 17, 2022 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. Commented Jun 17, 2022 at 10:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.