How to reroute stdout, stderr back to /dev/tty How to reroute stdout, stderr back to /dev/tty linux linux

How to reroute stdout, stderr back to /dev/tty


A command that should do literally what you asked for in (2) is

exec >/dev/tty 2>&1

But I suspect that your analysis of the problem is incorrect. It would be useful to see the output of ssh -v ... (where ... is whatever arguments you typed in your original ssh command).


The command:

ls -l /proc/$$/fd/{1,2}

will show you which files are open as stdout (file descriptor 1) and stderr (file descriptor 2).


An answer to your first question could be found in /proc/self/fd. It contains symlinks to the files (or other things, pipes, sockets, etc) that your bash instance is connected to.

root@mammon:~# ls -l /proc/self/fdtotal 0lrwx------ 1 root root 64 May 21 02:18 0 -> /dev/pts/3lrwx------ 1 root root 64 May 21 02:18 1 -> /dev/pts/3lrwx------ 1 root root 64 May 21 02:18 2 -> /dev/pts/3lr-x------ 1 root root 64 May 21 02:18 3 -> /proc/15529/fd/root@mammon:~# ls -l /proc/self/fd < /dev/nulltotal 0lr-x------ 1 root root 64 May 21 02:18 0 -> /dev/nulllrwx------ 1 root root 64 May 21 02:18 1 -> /dev/pts/3lrwx------ 1 root root 64 May 21 02:18 2 -> /dev/pts/3lr-x------ 1 root root 64 May 21 02:18 3 -> /proc/15536/fd/root@mammon:~# ls -l /proc/self/fd | cattotal 0lrwx------ 1 root root 64 May 21 02:18 0 -> /dev/pts/3l-wx------ 1 root root 64 May 21 02:18 1 -> pipe:[497711]lrwx------ 1 root root 64 May 21 02:18 2 -> /dev/pts/3lr-x------ 1 root root 64 May 21 02:18 3 -> /proc/15537/fd/root@mammon:~#

In the first example, you can see the first 3 file descriptors (which are the standard output, input, and error, respectively) all point to my pseudo-terminal /dev/pts/3. In the second example I've redirected the input to /dev/null, so the standard input file descriptor points to /dev/null. And in the final example I've sent the output of ls to cat through a pipe, and the standard input file descriptor reflects this. As far as I know there is no way to find which process has the other end of the pipe. In all examples there is the fourth file descriptor that represents the handle that ls has for reading /proc/self/fd. In this case it says /proc/15537 because /proc/self is in fact a symlink to /proc/pid where pid is the PID of the process accessing /proc/self.