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 dnf Command: Manage RPM Distribution Packages

Linux dnf Command: Manage RPM Distribution Packages

Learn how to manage RPM Distribution Packages with the Linux dnf command, including practical examples, key options, and important precautions.

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Learn how to inspect and Configure Network Interfaces and Routes with the Linux ip command, including practical examples, key options, and important precautions.

Linux bunzip2 Command: Decompress bzip2 Files

Linux bunzip2 Command: Decompress bzip2 Files

Learn how to decompress bzip2 Files with the Linux bunzip2 command, including practical examples, key options, and important precautions.

Linux locate Command: Search Indexed File Paths Quickly

Linux locate Command: Search Indexed File Paths Quickly

Learn how Linux locate searches a filename database, how updatedb affects freshness, how to limit and filter matches, and when find is the better tool.

Linux Tutorial / Compare Files with diff and cmp

Linux Tutorial / Compare Files with diff and cmp

Use diff and cmp to compare two files, read unified diff output, and distinguish a detected difference from an actual command error.

Linux sync Command: Flush Buffered Writes to Storage

Linux sync Command: Flush Buffered Writes to Storage

Learn how to flush Buffered Writes to Storage with the Linux sync command, including practical examples, key options, and important precautions.

Linux cut Command: Extract Characters and Fields

Linux cut Command: Extract Characters and Fields

Learn how to extract Characters and Fields with the Linux cut command, including practical examples, key options, and important precautions.

Linux fsck Command: Check and Repair Filesystems

Linux fsck Command: Check and Repair Filesystems

Learn how to check and Repair Filesystems with the Linux fsck command, including practical examples, key options, and important precautions.

Linux getent Command: Query Users, Groups, and Other NSS Databases

Linux getent Command: Query Users, Groups, and Other NSS Databases

Learn how to query Users, Groups, and Other NSS Databases with the Linux getent command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.