YouTube Icon

Code Playground.

How to Redirect stderr to stdout in Bash

CFG

How to Redirect stderr to stdout in Bash

While diverting the yield of an order to a document or funneling it to another order, you may see that the blunder messages are imprinted on the screen. 

In Bash and other Linux shells, when a program is executed, it utilizes three standard I/O streams. Each stream is spoken to by a numeric document descriptor: 

  • 0 - stdin, the standard info stream. 
  • 1 - stdout, the standard yield stream. 
  • 2 - stderr, the standard mistake stream. 

A record descriptor is only a number speaking to an open document. 

The info stream gives data to the program, by and large by composing in the console. 

The program yield goes to the standard information stream and the blunder messages goes to the standard mistake stream. Of course, both information and blunder streams are imprinted on the screen. 

Redirecting Output

Redirection is an approach to catch the yield from a program and send it as contribution to another program or document. 

Streams can be diverted utilizing the n> administrator, where n is the document descriptor number. 

At the point when n is precluded, it defaults to 1, the standard yield stream. For instance, the accompanying two orders are the equivalent; both will divert the order yield (stdout) to the document. 

command > file
command 1> file

To divert the standard blunder (stderr) utilize the 2> administrator: 

command 2> file

You can compose both stderr and stdout to two separate records: 

command 2> error.txt 1> output.txt

To stifle the mistake messages from being shown on the screen, divert stderr to/dev/invalid: 

command 2> /dev/null

Redirecting stderr to stdout

When sparing the program's yield to a document, it is very basic to divert stderr to stdout with the goal that you can have everything in a solitary record. 

To divert stderr to stdout and have blunder messages shipped off a similar document as standard yield, utilize the accompanying: 

command > file 2>&1

> record divert the stdout to document, and 2>&1 divert the stderr to the current area of stdout. 

The request for redirection is significant. For instance, the accompanying model diverts just stdout to record. This happens in light of the fact that the stderr is diverted to stdout before the stdout was diverted to document. 

command 2>&1 > file 

Another approach to divert stderr to stdout is to utilize the &> develop. In Bash &> has a similar importance as 2>&1: 

command &> file

Conclusion

Understanding the idea of redirections and document descriptors is significant when taking a shot at the order line. 

To divert stderr and stdout, utilize the 2>&1 or &> builds. 

On the off chance that you have any inquiries or criticism, don't hesitate to leave a remark.




CFG