Standard error (stderr) is one of the three standard data streams in Linux, specifically designed for error messages and diagnostic output.
It uses file descriptor 2 and by default displays on the terminal screen, separate from standard output (stdout).
Purpose of stderr
Unlike stdout (file descriptor 1) which carries normal program output, stderr handles error messages, warnings, and diagnostic information. This separation allows users to redirect normal output while still seeing error messages, or vice versa.
Basic stderr Examples
Viewing stderr Output
ls /nonexistent_directory
# Error message appears on screen via stderr
ls: cannot access '/nonexistent_directory': No such file or directory
Command with Both stdout and stderr
find /home -name "*.txt"
# Files found go to stdout
# Permission denied errors go to stderr
Redirecting stderr
Redirect stderr to File (2>)
find /root -name "*.txt" 2> errors.log
# Normal output to screen, errors to file
Append stderr (2>>)
command 2>> error_log.txt
# Adds errors to existing file
Redirect stderr to stdout (2>&1)
ls /home /invalid 2>&1 | grep "Permission"
# Both outputs combined for piping
Discard stderr (2>/dev/null)
find / -name "*.conf" 2>/dev/null
# Suppress error messages completely
Practical Applications
Logging with Error Separation
backup_script.sh > backup.log 2> backup_errors.log
# Success messages and errors in separate files
Silent Execution
crontab_job > /dev/null 2>&1
# Suppress all output (common in cron jobs)
Error Checking in Scripts
if ! command 2>/dev/null; then
echo "Command failed" >&2 # Send to stderr
exit 1
fi
Debugging Output
echo "Debug: Processing file $filename" >&2
# Debug messages to stderr, preserving stdout for data
Understanding stderr is essential for effective error handling, logging, and creating robust scripts that properly separate normal output from diagnostic information.