If you wanted to start the command in background and have a file descriptor to send data to it via a pipe.
With zsh
or bash
, you could use a redirection to a process substitution.
exec 3> >(cmd)
And then send output with:
echo something >&3
And tell end-of-file with exec 3>&-
.
Sill with zsh
and bash
, you can also do:
{ coproc cmd >&3 3>&-; } >&3
That is start cmd
as a co-process, which starts the process with two pipes, one for input, one for output, but here as we only want the one for input, we restore cmd
's stdout to that of the rest of the shell.
To send output:
echo something >&p # with zsh
echo something >&"${COPROC[1]}" # with bash
With yash
, you can use process redirection:
exec 3>(cmd)
With any Bourne-like shell, you can always do:
{ {
echo something >&3
echo whatever
...
} 3>&1 >&4 4>&- | cmd; } 4>&1
That's not exactly the same as starting cmd
in background and run commands that send their output to cmd
in that those commands run in a subshell and, for interactive shells, cmd
is also in the foreground (is affected by Ctrl+C for instance), but apart from that, it's mostly functionally equivalent.
[1]+ Stopped
. That's because your program has received aSIGTTIN
signal, for trying to read from the terminal while running in background. – Satō Katsura Aug 05 '17 at 16:37