I would like to ask if it is possible to set up in bash so that each command is executed from of the top of terminal window. In other words if it is possible to execute clear before each command.

Also I wonder if it is possible to do this without using preexec() hook since I have history disabled anyway in bash.

marked as duplicate by αғsнιη, Scott, Romeo Ninov, Jeff Schaller, Archemar Jul 30 '17 at 13:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Out of interest, why would you want to do this? Any command with any kind of output would be moot, e.g. ls. – Sparhawk Mar 14 '17 at 2:23
  • 1
    Put ^[[2J^[[;H at the beginning of PS1, and bask in the inability to see the output of any of your commands. (Note: ^[ indicates a literal Escape) – DopeGhoti Mar 14 '17 at 2:35
  • or just do PS1=`clear` ; very effective – Rui F Ribeiro Mar 14 '17 at 2:54
  • A cat with many skins, POSIX be. – DopeGhoti Mar 14 '17 at 3:36
  • 1
    Not exactly the same, but related and more useful: Keep bash input on top line of screen – Gilles Mar 14 '17 at 23:35

You could do this on your bashrc:


According to the Bash manual:


If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

So every time you press Enter to commit a command, Bash will clear the screen for you after executing the command. You won't be able to see the output of your commands though.

  • No, OP wants to clear before command to be executed so output and the command itself must be there, not after commands got executed immediately clear the screen! – αғsнιη Jul 30 '17 at 6:48
  • Hmm, yeah I got it all wrong. The event to clear the screen would be, "as soon as I start typing, the screen is cleared", or something like that? – Leonardo Gomes G. Avellar Jul 30 '17 at 6:57
  • see this Q&A unix.stackexchange.com/q/44713/72456 – αғsнιη Jul 30 '17 at 7:39
  • OP doesn't want to use the preexec. preexec would be my primary answer. – Leonardo Gomes G. Avellar Jul 30 '17 at 9:17
  • S/he can refer to another two answers : ) – αғsнιη Jul 30 '17 at 9:30

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