Let's Discuss STDIN, STDOUT and STDERR
Dipto Mondal
CTO ? Ex-Pathao ? Backend Engineer ? System Architect ? Distributed Systems ? Golang ? Rust ? Microservices ? Google cloud ? Kubernetes ? Docker ? Kafka ? Clickhouse ? Building Next Generation Trading Applications
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
Software Engineer at Pathao
3 年nice article Dipto Mondal