How to Echo to Stderr in Bash

MD Aminul Islam Feb 02, 2024
  1. Use the >&2 Operator to Echo to stderr in Bash
  2. Use echo With File Descriptor Redirection to Echo to stderr in Bash
  3. Use printf With File Descriptor Redirection to Echo to stderr in Bash
  4. Use echo With Process Substitution to Echo to stderr in Bash
  5. Use the logger -s Command to Echo to stderr in Bash
  6. Use the /dev/stderr File to Echo to stderr in Bash
  7. Use the /proc/self/fd/2 File Descriptor to Echo to stderr in Bash
  8. Conclusion
How to Echo to Stderr in Bash

In Bash scripting, the standard output (stdout) is typically used to display regular program output, while the standard error (stderr) is reserved for displaying error messages. However, there are scenarios where you may want to print messages directly to stderr.

The Linux environment identifies every file object with its description, known as FD. It is a positive integer value that identifies the open file session. For stderr, the value of the file descriptor is 2.

This article will show different methods to redirect the echo output to stderr in Bash.

Use the >&2 Operator to Echo to stderr in Bash

The command stderr is mainly used to keep a record of errors during the execution of any command.

Syntax:

Your command here 2>error.log

In the syntax above, you can find a symbol 2> and a file named error.log. Now the 2> indicates the value of FILE DESCRIPTOR, representing the identity of stderr.

In our case, the FILE DESCRIPTOR value is 2. We provided the file name for the log file generated during the command execution.

You can also echo the stderr message by following the below syntax.

Your command here >&2 echo "error"

Let’s see an example to make the topic easier. Suppose we have created an error command and want to echo the output error message.

echo $( 3 + 2 )) >&2 echo "error"

We intentionally made an error in the code above to understand how it works. We removed one of the parentheses here, which can be an error.

After that, we show that error with the echo.

Output:

main.bash: line 6: syntax error near unexpected token `)'
main.bash: line 6: `echo $( 3 + 2 )) >&2 echo "error"'

Now we correct the code like below and run the command again.

echo $(( 3 + 2 )) >&2 "No error has been found."

Output:

5 No error has been found.

Use echo With File Descriptor Redirection to Echo to stderr in Bash

This method involves temporarily redirecting stdout to stderr.

echo "This is regular output"
exec 1>&2
echo "This is an error message"
  1. echo "This is regular output": This line prints "This is regular output" to stdout.
  2. exec 1>&2: This line redirects stdout (file descriptor 1) to stderr (file descriptor 2).
  3. echo "This is an error message": Since stdout is now redirected to stderr, this line prints "This is an error message" to stderr.

Use printf With File Descriptor Redirection to Echo to stderr in Bash

This method utilizes the printf command along with file descriptor redirection.

echo "This is regular output"
printf "This is an error message\n" 1>&2
  1. echo "This is regular output": This line prints "This is regular output" to stdout.
  2. printf "This is an error message\n" 1>&2: The printf command is used to format and print the error message. The 1>&2 redirects stdout to stderr.

Use echo With Process Substitution to Echo to stderr in Bash

Process substitution allows you to use a command’s output as a file.

echo "This is regular output"
echo "This is an error message" > >(cat 1>&2)
  1. echo "This is regular output": This line prints "This is regular output" to stdout.
  2. echo "This is an error message" > >(cat 1>&2): The > >(cat 1>&2) construct sends the output of the echo command to the cat command, which in turn redirects it to stderr (1>&2).

Use the logger -s Command to Echo to stderr in Bash

The logger -s command is a convenient way to send output to the system log, which typically goes to stderr.

Here’s an example code:

echo "This is regular output"
logger -s "This is an error message"
  1. echo "This is regular output": This line prints "This is regular output" to stdout.
  2. logger -s "This is an error message": The logger -s command logs the message "This is an error message". The -s flag ensures that the message is sent to stderr.

When you run this script, the error message will be logged to the system log, typically directed to stderr.

Use the /dev/stderr File to Echo to stderr in Bash

You can use the /dev/stderr special file to redirect output to stderr. Here’s an example code:

echo "This is regular output"
echo "This is an error message" > /dev/stderr
  1. echo "This is regular output": This line prints "This is regular output" to stdout.
  2. echo "This is an error message" > /dev/stderr: Here, the > /dev/stderr part redirects the output of the echo command to the /dev/stderr special file, which represents stderr.

When you run this script, the error message will be sent directly to stderr.

Use the /proc/self/fd/2 File Descriptor to Echo to stderr in Bash

You can use the /proc/self/fd/2 file descriptor to redirect output to stderr. Here’s an example code:

echo "This is regular output"
echo "This is an error message" > /proc/self/fd/2
  1. echo "This is regular output": This line prints "This is regular output" to stdout.
  2. echo "This is an error message" > /proc/self/fd/2: Here, the > /proc/self/fd/2 part redirects the output of the echo command to the file descriptor 2, which represents stderr.

When you run this script, the error message will be sent directly to stderr.

Conclusion

This article explored seven methods to echo to stderr in Bash. Each method has its strengths and use cases, so choose the one that best suits your application.

Understanding these methods will give you greater flexibility in handling error messages in your Bash scripts.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Bash Echo