Clearing terminal

6

5

I would like to issue a command from a bash script to clear the terminal it is running from:

  • I don't want to clear the bash history (history -c)
  • I don't want to issue the clear command (which moves the terminal down to the last prompt, giving the impression the terminal has been cleared, but previous output remains visible if you scroll up)
  • I want to completely remove all previous output to my terminal and have it clean as if I was opening a new one

Thanks.

Max

Posted 2011-02-01T11:52:33.197

Reputation: 438

Answers

11

Try printf '\033c'. This sends an escape sequence to the terminal, usually clearing all contents, including scrollback buffer.

maxelost

Posted 2011-02-01T11:52:33.197

Reputation: 2 191

This is just a simple reset. Reset doesn't clear scrollback. – Let_Me_Be – 2011-02-01T13:16:47.307

@Let_Me_Be: I never experienced that it didn't. Tried just now -- works fine in xterm, rxvt and gnome-terminal. – maxelost – 2011-02-01T13:42:09.940

printf '\033c' works like a charm. Thanks a lot. – Max – 2011-02-02T07:56:13.053

2

clear DOES clear the terminal screen. I guess you're accessing the Terminal from a graphic interface and hence you're actually using an interface to the console, not the actuall console. This interface automatically "saves" your output history (The stuff you see when scrolling up). In a real console (no graphic interface), you can't just scroll up. In this case, the terminal is just a program that keeps logs of the console output.

If you open up a new Terminal window and close the current one, you'll start off with a fresh window. (You could also reinitialize manually by going to Terminal -> Reinitialize and clear")


Terminal running in Desktop Environment: enter image description here

Console without Desktop Environment: enter image description here

BloodPhilia

Posted 2011-02-01T11:52:33.197

Reputation: 27 374

1And how would you do that from a bash script? Btw., the virtual console has some kind of scrollback (shift-pgup/pgdn). – maxelost – 2011-02-01T13:09:23.430

@Maxelost You can not, the bash script can't manipulate the terminal window directly. The Terminal window merely "emulates" a Console. Terminal != Console. Everything to see when you scroll up in the Terminal window is a mere record the Terminal window keeps. It's not done by the Console. – BloodPhilia – 2011-02-01T13:10:33.750

But the OP doesn't want to clear the terminal, he wants to clear the history buffer. – Let_Me_Be – 2011-02-01T13:12:32.863

@Let_Me_Be as I'm trying to make clear, there is no native way. To clear the Terminal Window's history from bash – BloodPhilia – 2011-02-01T13:13:24.980

0

Check this SU post referencing the Bash reset and clear commands

In addition to reset the answer also describes one more way to clear terminal.

Utkarsh Naiknaware

Posted 2011-02-01T11:52:33.197

Reputation: 1

-1

You can't erase the terminal history (at least there is no portable way). This feature is usually reserved for the user only, since having history erased by some program would be extremely annoying.

If you want to clear the terminal, then probably the most portable approach is using tput clear (this will output a terminal dependent string that will clear the screen).

Let_Me_Be

Posted 2011-02-01T11:52:33.197

Reputation: 1 364

1On my OS X system, this is no different that clear. – Daniel Beck – 2011-02-01T12:51:52.437

Well, it shouldn't. The difference is that clear is a command, tput isn't a direct command, instead it generates a string that is then interpreted by the terminal. – Let_Me_Be – 2011-02-01T13:02:52.800

1tput clear will clear the screen, but not the scrollback. – mazianni – 2011-02-01T15:18:55.023

-1

One option might be to see if the terminal can be started with scrollback disabled. For example, xterm -sl 0 will start an xterm with 0 saveLines. See the man file:

   -sl number
           This option specifies the number of lines  to  save  that  have
           been  scrolled  off the top of the screen.  This corresponds to
           the saveLines resource.  The default is 64.

mazianni

Posted 2011-02-01T11:52:33.197

Reputation: 344