When I run

$ screen id

all I can see is [screen is terminating] message in the top of the screen. What I want to see is the output of the id command and the [screen is terminating] message. I can get his with:

$ TERM=vt102 screen id

In this case, screen(1) doesn't clear the screen, but emits many empty lines at the end, so I can scroll up and see the output of id.

However, I don't want to use TERM=vt102, because I need some more advanced terminal features.

How can I prevent screen(1) from clearing the screen when terminating?

  • Why are you using screen for that? Screen is a terminal wrapper, not a terminal. – sparticvs Aug 19 '15 at 15:07
  • @sparticvs: I'm using screen to keep the program running, and make it possible to reattach after a hangup. Hangups happen 1% of the time, the main use case is that the program runs for a couple of minutes without a hangup, and then exits (and then screen also exits). – pts Aug 20 '15 at 0:48
  • Use nohup instead. You will find it easier. – sparticvs Aug 20 '15 at 12:50

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)

  • Thank you for the insights. Indeed termcapinfo * ti=:te= does the trick. However, when I don't specify it (and keep the default), screen clears the screen upon exit (rather than restoring whatever was displayed before screen started). I want to prevent this explicit clearing from happening. – pts Jan 5 '19 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.