Handling Standard Error Streams

Learn how to handle standard error streams.

Sending output to another file

When errors occur in commands, we see both the standard output and standard error streams displayed on our screen. This makes it easier for us to see when something goes wrong, but it makes it more difficult for us to send the output to a file or another program. To get around this, well-behaved programs separate diagnostics messages and other error messages to the standard error stream and leave it up to us to split them out. As output, we use a file descriptor to either send the errors to a file or suppress the messages.

Since the machines on Educative’s platform have default root privileges, we’ll be creating a temporary user who doesn’t have them.

Let’s look at an example of this in action. Run the terminal below to use the find command to look at the contents of the /root folder, which we don’t have access to view:

su temp 
find /root/

Run the complete code on the terminal below for practice.

Get hands-on with 1200+ tech skills courses.