6

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.

5 Answers 5

11

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

3
  • This is just a simple reset. Reset doesn't clear scrollback. Feb 1, 2011 at 13:16
  • @Let_Me_Be: I never experienced that it didn't. Tried just now -- works fine in xterm, rxvt and gnome-terminal.
    – maxelost
    Feb 1, 2011 at 13:42
  • printf '\033c' works like a charm. Thanks a lot.
    – Max
    Feb 2, 2011 at 7:56
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

4
  • 1
    And how would you do that from a bash script? Btw., the virtual console has some kind of scrollback (shift-pgup/pgdn).
    – maxelost
    Feb 1, 2011 at 13:09
  • @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.
    – Pylsa
    Feb 1, 2011 at 13:10
  • But the OP doesn't want to clear the terminal, he wants to clear the history buffer. Feb 1, 2011 at 13:12
  • @Let_Me_Be as I'm trying to make clear, there is no native way. To clear the Terminal Window's history from bash
    – Pylsa
    Feb 1, 2011 at 13:13
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.

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

3
  • 1
    On my OS X system, this is no different that clear.
    – Daniel Beck
    Feb 1, 2011 at 12:51
  • 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. Feb 1, 2011 at 13:02
  • 1
    tput clear will clear the screen, but not the scrollback.
    – mazianni
    Feb 1, 2011 at 15:18
-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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .