2

From a bash script that occupies the full terminal, how do I restore the previous session?

A few programs of which I know they work this way:

  • ranger
  • vim
  • man
  • nano

The common pattern is: The program usurps the whole terminal, and then, upon exiting, everything is restored, i.e. entered commands and printed output.

tifrel
  • 307

2 Answers2

2

You should use the terminfo cup options. This can be done from a shell with the tput command.

eg

tput smcup
clear
echo hello
echo there
read
tput rmcup

This will clear the screen, print two lines, wait for you to press RETURN and then restore the screen to where it was before.

This requires the terminfo definition for your terminal to support this, of course. Not all terminals have the capability.

  • Works. Already figured out that googling for 'bash alternate screen' gives me the desired behaviour, while I tried 'bash restore session'. Thanks. – tifrel May 31 '18 at 08:47
0

Using clear or tput clear messes up the history of the original screen when used in a bash script. The screen is still there but only the visible screen. All the history is gone. It took me ages to find out that clear has to be replaced with tput cup 0 0 (works in my case by positioning the cursor upper left).

tput smcup
tput cup 0 0  # position cursor top left, simulate clear
...script
tput rmcup