Redirecting standard error in Bash

Standard error in Bash is the output thrown by the terminal that is basically a error. There are three types of standard streams that can be used by the process for input and output:

1. Standard Input – This is represented by 0. This is where program receive its input.

2. Standard Output – This is represented by 1. This is where program send its regular output.

3. Standard Error – This is represented by 2. This is useful for redirecting error messages to the logfile or anywhere else.

Look and observe the below image to understand the standard error and then we will learn how to redirect the stderr.

stderr in bash

ls: This command list all the contents that are in the directory.

$ ls not_exist_directory

Here we “ls” the “not_exit_directory” that is not available hence resulting in generating the error. And, when we use “echo $?” we got the exit code of “2” that is standard error and we can redirect it so we can deal with it.

There are two ways to redirect the Standard Error in Bash:

1. /dev/null :This directory is like the blackhole of the linux system. Whichever goes in here just vanish from existence. This is useful when we don’t want to store the standards error.

2. Text FIles : We can create a text file and then redirect standard error in the text file so we can use and analyze it later.

Redirecting Stderr in /dev/null

Now lets redirect the standard errors in this blakhole and only get what we wanted to see that is the clean and correct output of the code/command.

$ find /etc -type f

find command

commands used:

find: this command in linux is used to find files or to get the files that are there in the system. Here “/etc” defines that find all the files in directory name “/etc” and “-type f” represents that print the only the files not the directory.

Output:

stderr

The output also contains error while we use “find” command to display all the files present in the “/etc” directory.

Redirecting Stderr in a text file

Now we are going to redirec the error that is thrown by “find” command and then we can analyze the errors later.

 $ find /etc -type f 2> errors.log

redirecting the error

Explanation:

2> errors.log” – This statement creates a files with name “errors.log” and “>” operator will overwrite all the contents of the file with the errors that are thrown by the “find” command.

Output:

stdout

It will print all the files but without any errors as all the error are redirected to the file “errors.log”.

Now let’s check the “errors.log” file and see what’s inside it.

Stderr in the errors.log file

As we can see all the error have been successfully redirected to the “errors.log” and now we can analyze later and thus we get the clean output of the code without getting any error. This shows how helpful redirection of standard error is. That’s why it is a good practise to handle error while writing a bash script to improve its efficiency.

How to Redirect Standard (stderr) Error in Bash

Bash (Bourne Again SHell) is the scripting language that is generally used to automate tasks. It is a Linux shell and command language written by Brain Fox. Bash is the default shell for Linux and macOS operating systems.

Similar Reads

What is a Standard Error in Bash?

When a code is run in Bash shell and if the code executes successfully it would return the exit code of 0. And, if the code fails or interrupts in between it would return the exit code of any non-zero positive number from 1-133....

Redirecting standard error in Bash

Standard error in Bash is the output thrown by the terminal that is basically a error. There are three types of standard streams that can be used by the process for input and output:...

Conclusion

Redirection of the error while writing bash script is the important practise as bash script work is to automate tasks, hence it is important to deal with standard errors and include code that deal with the errors too. Bash language is a fascinate language to learn and work with. Linux users and those who are dealing with servers in MNCs have to work with bash to automate tasks....