Overview
The clear command clears the terminal screen and moves the cursor to the top-left position. It provides a clean workspace by removing all previous output from view.
Syntax
Description
The clear command sends ANSI escape sequences to the terminal to:
Clear the entire screen (\x1b[2J)
Move the cursor to the home position (top-left: \x1b[H)
This effectively gives you a fresh terminal view, though the command history is preserved.
Options
The clear command does not accept any options, flags, or arguments.
Examples
Basic Usage
clear
# Screen is cleared, cursor moves to top-left
Clear Between Tasks
ls -la
cat file.txt
grep pattern data.txt
# Clear screen before starting new task
clear
echo "Starting new task"
Clear in Scripts
# script.sh
echo "Processing data..."
cat data.txt | grep important
# Clear before showing results
clear
echo "Results:"
grep -c important data.txt
Practical Use Cases
Clean Workspace
# After lots of output, clear for readability
find / -name "*.txt"
# ... lots of output ...
clear
echo "Ready for next command"
Script Sections
# Separate script sections visually
echo "Step 1: Preparation"
mkdir -p output
clear
echo "Step 2: Processing"
cat input.txt | grep data > output/result.txt
clear
echo "Step 3: Complete"
ls -l output/
# Simple menu script
while true ; do
clear
echo "=== Main Menu ==="
echo "1. Show files"
echo "2. Show environment"
echo "3. Exit"
echo ""
# ... handle input ...
done
Before Important Output
# Clear before showing important information
clear
echo "======================"
echo " CRITICAL ALERT"
echo "======================"
echo "System status: ERROR"
Development Workflow
# Clear before each test run
clear
echo "Running tests..."
# run tests
Demonstration Scripts
# Demo script with clear sections
clear
echo "Demo: File Operations"
echo ""
echo "Creating files..."
touch demo1.txt demo2.txt
ls -l demo * .txt
echo ""
echo "Press enter to continue"
read
clear
echo "Demo: Text Processing"
echo ""
echo "hello world" | grep hello
Terminal Behavior
The clear command:
Does clear: Visible terminal output
Does clear: Screen contents
Does NOT clear: Command history (use history to view)
Does NOT clear: Scrollback buffer (may vary by terminal)
Does NOT clear: Environment variables
Does NOT clear: Working directory
ANSI Escape Sequences
Under the hood, clear outputs:
\x1b[2J - Clear entire screen
\x1b[H - Move cursor to home (1,1)
This is equivalent to:
Alternative Methods
clear command
echo with ANSI
Multiple newlines
clear
# Simplest and most portable
echo -e "\x1b[2J\x1b[H"
# Manual escape sequence
echo "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
# Poor alternative (doesn't truly clear)
Combining with Other Commands
Clear and show directory
Clear and show status
Clear between operations
Clear and display info
In Loops and Scripts
Updating Display
# Simulate updating display
for i in 1 2 3 4 5 ; do
clear
echo "Processing step $i /5..."
# simulate work
done
clear
echo "Complete!"
Progress Indicator
# Show progress on clean screen
COUNT = 0
while [ " $COUNT " -lt 10 ]; do
clear
echo "Progress: $COUNT /10"
COUNT = $(( COUNT + 1 ))
done
Common Patterns
Clear Before Error Messages
if test ! -f config.json ; then
clear
echo "ERROR: config.json not found"
exit 1
fi
Clear After Verbose Output
find . -name "*.log" # potentially lots of output
clear
echo "Search complete"
Clear for User Interaction
clear
echo "Enter your name:"
read NAME
echo "Hello, $NAME "
Terminal Compatibility
The clear command uses ANSI escape sequences that are supported by most modern terminals:
✅ xterm, xterm-256color
✅ VT100/VT220 terminals
✅ Modern terminal emulators (iTerm2, Terminal.app, GNOME Terminal, etc.)
❌ Very old or non-ANSI terminals may not support this
Nash defaults to TERM=xterm-256color, so clear should work in most environments.
Most terminals maintain a scrollback buffer: echo "line 1"
echo "line 2"
echo "line 3"
clear
# Screen is clear, but you can scroll up to see previous lines
The clear command does not clear the scrollback buffer—only the visible screen.
Shell State Preservation
After clear, all shell state is preserved:
export MY_VAR = value
cd /home/user/projects
clear
# State is preserved
echo $MY_VAR # Output: value
pwd # Output: /home/user/projects
history | tail -5 # Shows commands before clear
Notes
The clear command has no options or arguments
It always returns exit code 0 (success)
Command history is not affected
Environment variables remain unchanged
Current directory remains unchanged
Useful for script readability and organization
Does not affect terminal scrollback buffer
Exit Status
The clear command always returns exit code 0 (success).
echo - Print text to screen
history - View command history
help - Display help information