Linux clear Command: Clear the Terminal Display

clear is a command that clears the terminal screen to tidy up the workspace. In a typical ncurses implementation, it attempts to clear not only the current screen but also the scrollback buffer if the terminal supports it, and using the -x option allows you to preserve the scrollback.

clear does not delete command history or files. Since it only changes the display on the screen, previous commands can usually be reviewed again using the shell's history function.

What is the clear command?

clear checks the type of terminal from the TERM environment variable and uses the capability information in the terminfo database to output the appropriate control sequences. Therefore, it is different from simply printing several blank lines, and its detailed behavior can vary depending on the terminal type and implementation.

The option descriptions in this article are based on the version of clear in ncurses, which is widely used on Linux.

Basic Syntax

clear [-x] [-T terminal_type]
clear -V

The most common usage is to run it without any options.

clear

What clear removes and does not remove

Target Action
Current terminal screen Clears the screen and moves the cursor to the initial position.
Scrollback buffer If the terminal and ncurses support the relevant features, the default execution will attempt to clear it. You can prevent this behavior with -x.
Shell command history Does not delete. You can check again with history or the Up arrow key.
Source of command output Does not affect log files or data generated by commands.
Shell variables and current directory Does not change.

Leave scrollback -x

clear -x

-x clears the screen but does not attempt to clear the scrollback buffer. It is useful if you want to tidy the screen and then scroll back to previous output.

This option is a feature of ncurses clear. It may not be supported on other operating systems or implementations, so check clear -V or the system manual.

Specify terminal type -T

clear -T xterm-256color

In general, clear automatically retrieves the terminal type from the TERM environment variable, so -T is not necessary. It is only explicitly used when diagnosing terminal detection issues or generating control sequences for a specific type.

printf '%s\n' "$TERM"
infocmp "$TERM"

If you specify a type that does not match the actual terminal, the screen may be cleared unexpectedly or control characters may appear.

Option summary

Option Description
-x The action to clear the scrollback buffer is omitted.
-T type Uses the functionality information of the specified terminal type instead of TERM.
-V Displays the linked ncurses version and exits.

Difference between Ctrl+L and clear

In the default Readline key settings of Bash, Ctrl+L redraws the screen and moves the current input line to the top. It is convenient for quickly clearing the screen, but it cannot be guaranteed that the way the terminal handles scrollback is the same as clear.

Method Advantages Features
clear Uses terminal functionality information and allows option selection. Depending on the implementation, you can even clear the scrollback.
clear -x It is easy to return to previous output while clearing the screen. It is an option in the ncurses implementation.
Ctrl+L It can be executed quickly from the keyboard while typing. Behavior may vary depending on the shell's line editing settings and the terminal.

Difference between clear and reset

clear is used to clear the screen of a terminal that is functioning normally. On the other hand, reset is used to reinitialize the terminal state when character display, input mode, or line breaks are broken due to binary output or incorrect control sequences.

reset

If the screen is simply cluttered, clear is faster, while reset is more suitable when typed characters are not visible or the display mode is broken.

Clearing the screen with tput

tput can also use the terminfo database to output terminal capabilities.

tput clear

It is useful when you need to combine terminal control features in shell scripts. However, in non-interactive tasks where output is sent to a file or a pipe, the control sequences can remain as unnecessary data, so it is recommended to check the terminal connection.

When using in scripts

Unless it is an interactive program like a dashboard that repeatedly updates the screen, it is better for scripts not to clear the user's screen arbitrarily. This is because it becomes difficult to compare error messages and previous results, and control characters may mix in log collection environments.

if test -t 1; then
  clear
fi

test -t 1 checks if the standard output is connected to a terminal. If the goal is screen refresh, also consider watch or terminal UI tools suitable for your purpose.

Troubleshooting

TERM environment variable not set

This can occur if TERM is not set or the command is run in an abnormal non-interactive environment.

printf '%s\n' "$TERM"

Rather than arbitrarily specifying a value without knowing the actual terminal type, it is better to configure SSH, containers, or the execution environment to pass the TERM value.

unknown terminal type

The terminfo entry corresponding to the TERM value may not exist on the system. Check with infocmp "$TERM", and either install the necessary terminal information package or configure the client to send a compatible value.

Previous output is still visible even after clear.

The terminal emulator may not support scrollback clearing, or the clear implementation you are using may only be able to clear the screen. This is separate from whether the command history has been deleted.

Frequently Asked Questions

Does running clear also delete the history?

No. clear changes the terminal display but does not delete Bash's command history. History deletion is handled by separate functions and settings of history.

Can I clear only the screen while keeping scrollback?

In ncurses implementations, you can use clear -x. Check if your system's implementation supports this option.

Is there a risk that clear will delete files?

The normal clear command only outputs terminal control sequences and does not delete files. However, an alias or function with the same name may have been redefined, so if you are suspicious, check with type -a clear.

Related articles and official documentation

More in This Category
Linux uptime Command: Check Uptime and Load Averages

Linux uptime Command: Check Uptime and Load Averages

Learn how to use the Linux uptime command to check how long the system has been running and interpret load averages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

Linux tail Command: View the End of a File and Follow Logs

Linux tail Command: View the End of a File and Follow Logs

Learn how to use the Linux tail command to view the last lines of a file and follow log updates in real time, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.

Linux mkfs Command: Create a New Filesystem

Linux mkfs Command: Create a New Filesystem

Learn how to create a New Filesystem with the Linux mkfs command, including practical examples, key options, and important precautions.

Linux cp Command: Copy Files and Directories

Linux cp Command: Copy Files and Directories

Learn how to copy files and directories with Linux cp, control overwrites, preserve attributes, handle symlinks, and verify important copies.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

Linux find Command: Search Files by Name, Type, Size, and Time

Linux find Command: Search Files by Name, Type, Size, and Time

Learn how to search with Linux find by name, file type, size, and modification time, combine expressions, run commands safely, and verify targets before deletion.

Linux tr Command: Translate and Delete Characters

Linux tr Command: Translate and Delete Characters

Learn how to translate and Delete Characters with the Linux tr command, including practical examples, key options, and important precautions.