Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


share|improve this question
up vote 11 down vote accepted

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

share|improve this answer
This is just a simple reset. Reset doesn't clear scrollback. – Let_Me_Be Feb 1 '11 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 '11 at 13:42
printf '\033c' works like a charm. Thanks a lot. – Max Feb 2 '11 at 7:56

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

share|improve this answer
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 '11 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. – BloodPhilia Feb 1 '11 at 13:10
But the OP doesn't want to clear the terminal, he wants to clear the history buffer. – Let_Me_Be Feb 1 '11 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 – BloodPhilia Feb 1 '11 at 13:13

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.

share|improve this answer

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

share|improve this answer
On my OS X system, this is no different that clear. – Daniel Beck Feb 1 '11 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. – Let_Me_Be Feb 1 '11 at 13:02
tput clear will clear the screen, but not the scrollback. – mazianni Feb 1 '11 at 15:18

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.
share|improve this answer

You must log in to answer this question.

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