Note that screen
is a terminal emulator. So your question is a bit like asking how can I start xterm
from gnome-terminal
and have what was last displayed in xterm
visible in my gnome-terminal
when xterm
exits.
Now the difference between xterm
and screen
is that while xterm
uses the X protocol to draw its screen, screen
uses a host terminal.
By default, screen
will clear the screen of its host terminal to display its own emulated terminal and where available will use the alternate screen (before clearing) of that host terminal, so that upon termination, it can restore the host terminal state as it was before starting.
So what you see is not screen
clearing the screen upon leaving, but restoring the host terminal's main screen. The content of the screen
window that was last displayed is still there on the alternate screen. With xterm
, you can have a look at it by selecting Show alternate screen in the Ctrl+MiddleClick menu.
If the host terminal doesn't support an alternate screen (like vt102 ones), it obviously can't do that. Instead, it does nothing, which is basically what you want.
So, what you can do is tell screen
that the host terminal doesn't support an alternate screen. For that, you can add to ~/.screenrc
:
termcapinfo * ti=:te=
Which says: for all possible host terminals (*
, matched against $TERM
), override the termcap/terminfo
database to say that for those terminals, the escape sequences to enter or leave the alternate screen are the empty string.
ti
and te
do not exactly mean alternate screen. From https://www.gnu.org/software/termutils/manual/termcap-1.3/html_node/termcap_39.html:
ti
(smcup
in terminfo
)
String of commands to put the terminal into whatever special modes are needed or appropriate for programs that move the cursor nonsequentially around the screen. Programs that use termcap to do full-screen display should output this string when they start up.
te
(rmcup
in terminfo
)
String of commands to undo what is done by the ti
string. Programs that output the `ti' string on entry should output this string when they exit.
But that translates to the alternate screen for most terminal emulators (specically, in xterm
it's alternate screen and saving/restoring of the cursor position)
nohup
instead. You will find it easier.