Combining a redirect with a here document seems logical enough:$ bash > foo <<EOFecho HelloEOF$ less fooHelloBut with a pipe it behaves differently:$ bash | tee foo <<EOFecho HelloEOF$ less fooecho HelloWhat's also weird is if you immediately type 'history':$ bash | tee foo <<EOFecho HelloEOF$ history$ history1 ...That is, the first command still seems to be inside the newly created bash. What's going on? Why does the pipe behave differently?
Why do piping and redirection work differently with here documents?
bash;shell;io redirection;pipe;command history
You're redirecting tee's input, not bash's. Use:bash << EOF | tee fooecho HelloEOFA few more examples to illustrate how it works:bash << EO1 3<< EO\2 | tee 3<< EO3 foo 4<< EO4fed (as a deleted temp file open in read-only mode) to bash fd 0EO1fed to bash fd 3 without $variable and $(cmd) expansionEO2fed to tee fd 3(not that tee does anything with its fd 3)EO3fed to tee fd 4(see how, like any redirection operator 3<< END can appear anywhere onthe command line (for simple commands at least))EO4Redirecting compound commands:{ head -n2; wc -l; } << EOFfed as fd 0 for the whole command group$(ps -e)EOFOr:if head -n1; then  head -n2fi << EOF123EOFYou can reuse the same ending token:$ ls -l /proc/self/fd << E 3<< E >&2 | 3<< E 4<< E ls -l /proc/self/fdpipe heredoc> Epipe heredoc> Epipe heredoc> Epipe heredoc> Etotal 0lr-x------ 1 stephane stephane 64 Feb 24 12:50 0 -> /tmp/zshLiVzp3 (deleted)lrwx------ 1 stephane stephane 64 Feb 24 12:50 1 -> /dev/pts/5lrwx------ 1 stephane stephane 64 Feb 24 12:50 2 -> /dev/pts/5lr-x------ 1 stephane stephane 64 Feb 24 12:50 3 -> /tmp/zshFLbQ7T (deleted)lr-x------ 1 stephane stephane 64 Feb 24 12:50 4 -> /proc/25749/fd/total 0lr-x------ 1 stephane stephane 64 Feb 24 12:50 0 -> pipe:[70735808]lrwx------ 1 stephane stephane 64 Feb 24 12:50 1 -> /dev/pts/5lrwx------ 1 stephane stephane 64 Feb 24 12:50 2 -> /dev/pts/5lr-x------ 1 stephane stephane 64 Feb 24 12:50 3 -> /tmp/zshL3KBp3 (deleted)lr-x------ 1 stephane stephane 64 Feb 24 12:50 4 -> /tmp/zshcJjS7T (deleted)lr-x------ 1 stephane stephane 64 Feb 24 12:50 5 -> /proc/25748/fd/As to why the first history command is run in that new bash in:$ bash | tee foo <<EOFecho HelloEOF$ history$ history1 ...That is because bash stdin is still the terminal, but its stdout is a dead pipe (at the other end of the pipe, tee has redirected its stdin from that delete temp file, so the reading end of that pipe is closed).bash doesn't write its prompt or the echo of what you type on its stdout (the dead pipe), but to the terminal, so that bash will let you enter history at a first prompt.However, that history (which is a builtin, so executed by the bash process, not a child), will write its output to that dead pipe. When doing so, the bash process will receive a SIGPIPE signal (since there's no reader) and die.The next prompt will be issued by the calling shell.