Send errors to STDERR

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the logging category.

Last Updated: 2024-04-25

Whenever printing error messages from shell scripts, make sure to redirect to STDERR, otherwise callers will not be able to react correctly.

Here is what you should do

echo "$lesson not found. Please contact customer support" >&2

Here's how it works: &2 copies the destination of file descriptor #2 (STDERR by default). The > tells echo where to send the output that is usually STDOUT (file descriptor #1). Bringing it together, this means echo's standard output gets sent to STDERR.

Resources