clear - Clear Terminal Screen

1. Introduction

The clear command in Linux and Unix-like operating systems is a fundamental utility designed to clear the terminal screen, bringing the command prompt to the top. This creates a clean workspace, which is especially helpful when the terminal is cluttered with previous commands, outputs, or error messages.

Contrary to what it might seem, clear typically does not delete the command history; it scrolls the previous output out of view, and in many terminal emulators, you can scroll up to review past content. The command relies on the TERM environment variable and the terminfo database to send the correct escape sequences for clearing the screen, making its behavior dependent on the terminal emulator in use, such as GNOME Terminal, xterm, or PuTTY.

  • Difference between clear and deleting command history:

    • clear: The clear command is used to clear the visible text from the terminal screen, providing a clean slate with the cursor positioned at the top. It only affects the display and does not impact the command history. After running clear, you can still access previously executed commands using the history command or by pressing the up arrow key (depending on your terminal’s capabilities).

    • Deleting Command History: Deleting command history involves removing the record of previously executed commands. This can be done either for the current session or permanently, affecting the saved history file (typically ~/.bash_history in Bash). Once the command history is deleted, those past commands can no longer be recalled.

      • Example:

        • history -c: Clears the command history for the current session only.
          Warning: Does not affect the saved history file (~/.bash_history), so previous commands will still be available in new sessions.

        • history -c && history -w: Clears both the current session’s history and the saved history file.
          Clarification: This overwrites the history file with an empty history, effectively clearing it without deleting the file itself.

        • rm ~/.bash_history: Permanently deletes the history file, removing all saved commands.
          Warning: This action is irreversible and may not immediately affect other open sessions, which could rewrite the file upon closing.

2. Basic Syntax

The clear command has a simple syntax:

$ clear [OPTION]...
  • [OPTION]...: The command has few options, and it’s most commonly used without any.

In most cases, typing clear alone is sufficient to clear the terminal screen.

3. Core Use Cases with Examples

The clear command serves several practical purposes, from everyday terminal management to enhancing script outputs.

3.1 Clearing a Cluttered Terminal Screen

When your terminal is filled with commands and outputs, clear provides a quick way to declutter the screen.

Example:

Suppose your terminal looks like this after running several commands:

$ ls -l
total 8
drwxr-xr-x 2 user user 4096 May  3 22:00 my_project
-rw-r--r-- 1 user user   25 May  3 22:00 notes.txt
$ echo "Hello there"
Hello there
$ date
Sat May  3 22:01:15 IST 2025

Run the clear command:

$ clear

Output:

The screen will now be empty, with the command prompt at the top:

$

3.2 Using clear in Scripts for Better Output Presentation

In shell scripts, clear can be used to clear the screen before displaying a menu or important information, improving readability.

Example:

#!/bin/bash

# Clear the screen before showing the menu
clear

echo "============================"
echo "      MY AWESOME SCRIPT     "
echo "============================"
echo ""
echo "Please choose an option:"
echo "1. Run Task A"
echo "2. Run Task B"
echo "3. Exit"
echo ""
echo -n "Enter your choice: "
# read choice
# ... rest of the script

When this script runs, it clears the terminal and displays the menu neatly at the top.

3.3 Using ANSI Escape Sequences as an Alternative

In situations where clear might not work as expected or more control is needed, ANSI escape sequences can be used to clear the screen.

Example:

$ printf "\033c"

This command sends an ANSI escape sequence that resets the terminal and clears the screen, often including the scrollback buffer in many terminal emulators.

4. Key Options Explained (with Examples)

The clear command has a limited set of options, but they can be useful in specific scenarios.

4.1 -x (Do Not Attempt to Clear Scrollback Buffer)

Purpose: Many terminal emulators maintain a scrollback buffer, which stores lines that have scrolled off the screen. The standard clear command may attempt to clear this buffer, depending on the terminal. The -x option ensures that clear only clears the visible screen, preserving the scrollback buffer for later review.

Syntax: clear -x

Example:

# Generate some output to fill the screen and scrollback
$ ls -R /etc > /dev/null
$ echo "This is the last visible line before clear -x"

# Clear the screen without affecting the scrollback buffer
$ clear -x

Output:

The screen will be clear, but you can scroll up to see the previous output, including “This is the last visible line…“.

4.2 --help (Display Help)

Purpose: Displays a brief help message summarizing the usage and available options for the clear command.

Syntax: clear --help

Example:

$ clear --help

Output:

Usage: clear [OPTION]...
Clear the terminal screen.

      --help     display this help and exit
      --version  display version information and exit
      -V         display version information and exit
      -x         do not attempt to clear the scrollback buffer

4.3 --version or -V (Display Version Information)

Purpose: Shows the version number of the clear utility and exits.

Syntax: clear --version or clear -V

Example:

$ clear -V

Output:

clear (GNU ncurses) 6.4

5. Understanding the clear Command

If you’re curious about how the clear command works in Linux, this section explains it in simple terms. You don’t need to know this to use clear—it’s just for those who want to peek under the hood!

5.1 What Does clear Do?

The clear command wipes your terminal screen clean, moving the command prompt back to the top.

5.2 How Does It Work?

When you type clear, it sends special codes called ANSI escape sequences to the terminal. These codes tell the terminal what to do, like “clear the screen” or “move the cursor.” Normally, you don’t see these codes—they just work silently.

To see them, you can run clear | cat -v. This shows something like:

Output:

^[[H^[[2J^[[3J

Here’s what each part means:

  • ^[[H: Moves the cursor to the top-left corner.
  • ^[[2J: Clears the whole screen.
  • ^[[3J: Tries to clear the scrollback buffer (your scroll-up history), but it doesn’t work in every terminal.

When you use clear | cat -v, the screen doesn’t clear because the codes are shown as text instead of being acted on, due to piping clear’s output to ‘cat -v’ command which displays these non-printable sequences in a human-readable format, preventing the terminal from interpreting them, so the screen doesn’t actually clear.

With -x: clear -x | cat -v

Run this:

clear -x | cat -v

Output (example):

^[[H^[[2J
  • ^[[H: Moves the cursor to the top-left.
  • ^[[2J: Clears the screen.

The ^[[3J is gone, so the scrollback buffer isn’t cleared.

Key Takeaway

  • clear: Wipes the screen and scrollback (if supported).
  • clear -x: Wipes only the screen, keeps the scrollback.

Note: Codes might differ by terminal, but -x always skips clearing the scrollback.

6. Handling Special Cases

6.1 Non-Interactive Shells/Piping Output

When clear is used in a non-interactive shell or its output is piped to another command, it usually does not send screen-clearing control sequences.

Example:

$ clear | cat -v

Output:

The output might be empty or show minimal control characters, as clear detects that its output is not a terminal.

6.2 Different Terminal Emulators

The clear command’s behavior depends on the terminal type specified by the TERM environment variable and the terminfo database. An incorrect TERM setting or a missing terminfo entry can cause clear to fail or produce garbled output.

Example:

Check your terminal type:

$ echo $TERM

Common values include xterm, linux, vt100, etc.

6.3 Remote Sessions (SSH)

The clear command works seamlessly over SSH sessions, as the TERM variable is typically negotiated correctly by the SSH client and server, ensuring the correct escape sequences are sent.

6.4 Creating an Alias for Complete Clear

To clear both the screen and the scrollback buffer, you can create an alias using an ANSI escape sequence.

Example:

Add this line to your ~/.bashrc or ~/.bash_aliases file:

alias cls='printf "\033c"'

Reload the shell configuration:

$ source ~/.bashrc

Now, running cls will clear the screen and scrollback buffer completely.

7. Frequently Asked Questions (FAQ)

7.1 What is the main purpose of the clear command?

The clear command clears the visible content of the terminal screen, moving the prompt to the top for a cleaner workspace.

7.2 Does clear delete my command history?

No, clear only affects the display. Your command history remains intact and can be accessed using arrow keys or the history command.

7.3 Can I still see the output that was “cleared”?

In most terminal emulators, yes. You can scroll up to view the scrollback buffer unless it was cleared. Using clear -x ensures the scrollback buffer is preserved.

7.4 Is there a keyboard shortcut for clear?

Yes, Ctrl+L is a widely used shortcut in most terminal emulators and shells, equivalent to running clear.

7.5 How does clear actually work?

The clear command uses the TERM environment variable to identify the terminal type and consults the terminfo database to find the appropriate escape sequence for clearing the screen. It then sends these sequences to the terminal emulator.

7.6 Why doesn’t clear work sometimes or prints weird characters?

This usually happens if the TERM variable is set incorrectly or the terminfo entry is missing or corrupted. Check your TERM setting with echo $TERM.

7.7 What’s the difference between clear and reset?

The clear command only clears the screen and moves the cursor to the top. The reset command reinitializes the terminal, resetting settings to their defaults and potentially fixing display issues caused by incorrect control sequences or terminal misconfigurations. Use reset if you encounter display problems or need to restore the terminal to a known state.

7.8 How can I clear the terminal screen and scrollback buffer completely?

Use printf "\033c" to send an ANSI escape sequence that clears the screen and scrollback buffer in many terminal emulators. Alternatively, create an alias like cls='printf "\033c"' for convenience.

7.9 How do I clear the terminal history?

To clear the shell’s command history in Bash, use:

$ history -c

To delete the persistent history file, use:

$ rm ~/.bash_history

Be cautious, as this permanently deletes your command history

7.8 Why doesn’t the screen clear when I pipe clear?

Piping clear’s output to another command (like cat) sends the escape sequences to that command instead of the terminal. The terminal only clears if it receives and interprets those sequences directly. To clear the screen in a script, use clear without piping.

8. Conclusion

The clear command is a simple yet indispensable tool for maintaining a clean and organized terminal workspace in Linux. Its ease of use, combined with options like -x for preserving scrollback and alternatives like Ctrl+L or printf "\033c", makes it versatile for various scenarios. Understanding its reliance on the TERM variable and terminfo database provides deeper insight into its functionality, especially when troubleshooting issues. Whether you’re a beginner decluttering your terminal or a scriptwriter enhancing output presentation, clear is a reliable utility for everyday terminal management.

9. clear Command: Reference Table of Key Options

Option(s)DescriptionExample CommandUse Case
(none)Clear the terminal screenclearDefault action to get a clean terminal view
-xDo not attempt to clear the scrollback bufferclear -xClear only visible screen, keep scrollback
--helpDisplay help message and exitclear --helpGet quick usage information
--version or -VDisplay version information and exitclear -VCheck the version of the clear utility