Let's Discuss STDIN, STDOUT and STDERR

So what are stdin, stdout and stderr in linux or unix bash?

Whenever we run a command in terminal the terminal creates three data stream one for standard input, one for standard output and one for standard error.


STDIN : As said previously stdin is responsible for taking input in terminal. We use read command to take user input from keyboard. This read command is using stdin stream.

read        


STDOUT : When we run a command in the terminal, the output(not error output) is sent to the stdout and terminal prints it.

ls        


STDERR : STDERR is the stream where the error from a command is sent.

acommand        

In linux all the streams are taken as virtual files and like real files we can read from it or write in it. And to uniquely identify each of this virtual files a file descriptor number is assigned to each of them. The file descriptor for this three streams are given below:

STDIN -> 0

STDOUT -> 1

STDERR -> 2

So how can we read from this streams specifically? For example if we want to run a command and read from the may be, stderr stream and stdout stream separately.

We have discussed the file descriptor of these streams already.

If we want to write in a file from the stderr stream we simply use this command

anything 2> err.txt        

we can also read from multiple stream and write to multiple file

anything 2> err.txt 1> output.txt        

要查看或添加评论,请登录

Dipto Mondal的更多文章

  • Memory optimisation by re structuring GO struct

    Memory optimisation by re structuring GO struct

    Identifying the problem(may be this is not right to call it problem, this is standard behaviour of how cpu accesses…

  • Nodeport, TargetPort, Port

    Nodeport, TargetPort, Port

    Noobies like me get confused about what are nodeport , targetport and port and what are their usecases. Let's start by…

社区洞察

其他会员也浏览了