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.

2
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.

1
  • 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 8: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

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.