Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

5 Answers

up vote 10 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. –  user359650 Feb 2 '11 at 7:56
add comment

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
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 '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
add comment

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
add comment

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
1  
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
1  
tput clear will clear the screen, but not the scrollback. –  mazianni Feb 1 '11 at 15:18
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.