Clear terminal shell in Linux
clear
is one of the oldest commands which was first introduced in 1979 (freeBSD systems) Then it was ported into the unix system in 1985. clear
is one of the most used commands. clear
writes to the standard output. You can redirect the standard output to a file (which prevents clear
from actually clearing the screen), and later cat the file to the screen, clearing it at that point.
clear
command install
Debian based - apt install libncurses5-dbg
Alpine - apk add libncurses5-dbg
Arch Linux - pacman -S ncurses
CentOS - yum install ncurses
Fedora - dnf install ncurses
OS X - brew install ncurses
Raspbian - apt-get install libncurses5-dbg
Docker - docker run cmd.cat/clear clear
clear
command Syntax:
clear [-T type] [-V] [-x]
clear
command options:
-T , --type |
indicates the type of terminal. Normally this option is unnecessary, because the default is taken from the environment variable TERM. If -T is specified, then the shell variables LINES and COLUMNS will also be ignored. |
-V |
reports the version of ncurses which was used in this program, and exits. |
-x |
do not attempt to clear the terminal's scrollback buffer using the extended “E3” capability. |
- |
display this help and exit. |
-V |
output version information and exit. |
clear
command examples:
1. Clear the terminal ($TERM)
When clear
is used without any options or arguments, it uses $TERM (environment variable) by default to remove the scrollback buffer. This is exactly the same if you use the keyboard shortcut key CTRL-L in BASH shell (or other interactive shells).
Here, we have two different terminals. We will execute the clear
command on both terminals and write the standard output of the clear
command to a file. Now, if we try to read the contents of the file with the cat
command then it will again clear
the terminal. So, we need to use -A
option with the cat
command to write non-printable characters to the terminal. We can see the output is different for different terminals:
First terminal output:
$ echo $TERM
tmux-256color
$ clear > asd.txt
$ cat -A asd.txt
^[[H^[[J^[[3J
Second terminal output:
$ echo $TERM
screen
$ clear > clear.txt
$ cat clear.txt -A
^[[H^[[J
2. Clear the screen without deleting the terminal's scrollback buffer.
When clear
is used with -x
option, it clears the terminal screen but it does not delete the scrollback buffer. (It uses the $TERM variable by default.) Note:- Use of the enhanced "E3" capability to clear the terminal's scrollback buffer is not recommended.
Here, we have two different terminals. We will execute the clear
command on both terminals and write the standard output of the clear
command to a file. Now, if we try to read the contents of the file with the cat
command then it will again clear the terminal. So, we need to use -A
option with the cat
command to write non-printable characters to the terminal. We can see the output is same for both of the terminals:
First terminal output:
$ echo $TERM
tmux-256color
$ clear -x > asd.txt
$ cat -A asd.txt
^[[H^[[J
Second terminal output:
$ echo $TERM
screen
$ clear -x > clear_.txt
$ cat clear_.txt -A
^[[H^[[J
3. Indicate the type of terminal to clean ($TERM is default)
We can force the clear
command to execute it as a different terminal type. Since the environment variable TERM serves as the default, this option is typically not needed. The shell variables LINES and COLUMNS will also not be used if -T
is provided.
$ clear -T tmux > clear_TERM.txt
$ cat clear_TERM.txt -A
^[[H^[[J^[[3J
Conclusion
In this article, we explored one of the most useful commands, The clear
command clears your screen if this is possible, including its scrollback buffer (if the extended “E3” capability is defined). It writes some non-printable characters based on the terminal and the options used. clear
looks in the environment for the terminal type given by the environment variable TERM, and then in the terminfo database to determine how to clear the screen.