How can I spawn a new background process from a bash script and redirect the current script’s stdout/stderr to that process’s stdin using fd redirection? I know of a few ways to accomplish the same effect, such as using a named pipe or a temporary file, using coprocs, or running the whole script in a subshell and redirecting the ouptut of that to the other process, but I am looking for a simple one line IO redirection from within the script, in the same way I can do this: exec 2>&1
. Basically, I want to do something like this (the way the process is spawned can be changed of course):
#!/bin/bash zenity --text-info & # The process to receive this script's output # Do something to redirect this shell's stdout # to the background process's stdin echo "something" # This should be read by the background process
Advertisement
Answer
I found a solution to this using process substitution:
#!/bin/bash # Redirect stdout/stderr of current process to stdin of spawned process # The >() syntax evaluates to a file descriptor that is connected # to the new process's stdin exec &> >(zenity --text-info)