Reputation: 29066
In bash consider I did this stupid thing:
$ exec 1>&-
$ exec 2>&-
I am sure I have not yet lost the game (oops actually I lost The Game) and I can reconnect to stdout. The think is I don't know how.
First thing I tried is create a fifo and using another shell to monitor stdout of the first shell. It did not work, I don't know why:
$ mkfifo stdout
$ exec 1>stdout
$ echo "Hello stdout"
$ tail -f stdout
$ # nothing here
How can I reconnect my closed
STDOUT
andSTDERR
after all?
I know that a solution would be to save STDOUT before playing with it:
$ exec 3>&1
$ exec 1>&-
$ echo "Nothing will see this"
$ exec 1<&3 # Restoring stdout
Upvotes: 9
Views: 4620
Reputation: 3209
I am running this on an Ubuntu machine, so I am not sure if it'll work for you, but this is what I did:
$ exec 1>&0
$ exec 2>&0
Suddenly, I had STDOUT and STDERR reconnected. Magic!
Explanation: Running the following commands, we get the following output:
$ ls -l /dev/stdout
lrwxrwxrwx 1 root root 15 Jun 11 23:39 /dev/stdout -> /proc/self/fd/1
$ ls -l /proc/self/fd/1
lrwx------ 1 jay jay 64 Jun 22 01:34 /proc/self/fd/1 -> /dev/pts/10
$ ls -l /proc/self/fd/
total 0
lrwx------ 1 jay jay 64 Jun 22 01:35 0 -> /dev/pts/10
lrwx------ 1 jay jay 64 Jun 22 01:35 1 -> /dev/pts/10
lrwx------ 1 jay jay 64 Jun 22 01:35 2 -> /dev/pts/10
lr-x------ 1 jay jay 64 Jun 22 01:35 3 -> /proc/12224/fd
Since all three fd's point to the same thing, we can return them back to normal just by pointing to /dev/pts/10 which the exec 1>&0
and exec 2>&0
do
Upvotes: 8
Reputation: 241671
The simplest way, since you haven't closed stdin
, which is also connected to your terminal:
exec 2>&0 1>&0
You could also use /dev/tty
:
exec 2>/dev/tty 1>&2
You have to do that blind, since stderr
is where bash
echos what you type, and without stderr
, it can't echo.
Upvotes: 6