TL,DR:
script -c myprogram /dev/null </dev/null >/dev/null
You can't “redirect” /dev/tty
in the same sense that you can redirect standard output. Standard output is defined as a file descriptor. Programs write to whatever file is already open on file descriptor 1 when they start. Some operating systems offer /dev/stdout
as a file that's equivalent to standard output, but it's an “alias” for standard output. In contrast, /dev/tty
is a file name, which refers to the process's controlling terminal. If a program opens /dev/tty
, explicitly it opens /dev/tty
, and that can't be redirected.
What you can do is run the program with a controlling terminal that isn't the same as the controlling terminal of the program that runs it. A simple way to do this is with the script
command. In its simplest form:
script -c myprogram /dev/null >/dev/null
When myprogram
runs and opens /dev/tty
, this is a terminal provided by script
, not the terminal in which script
runs. What script
does when it detects a write on the terminal is to both write to its own standard output and write to the indicated typescript file; hence I set both script
's standard output and the typescript file to /dev/null
.
If myprogram
reads from the terminal, script
reads from its own standard input, so you'll probably want to redirect this to /dev/null
as well.
Note that script
does not pass the exit status of myprogram
to its caller. Some implementations (e.g. the one in Debian and derivatives) have a -e
option to do that.
/dev/tty
and I would want to redirect it to/dev/null
when running tests on it. I'll update the question.