Every process In Linux produces three data streams, “stdin,” “stdout,” and “stderr”:
- stdin: Takes input from the user via keyboard
- stdout: Displays output on the screen
- stderr: Shows error information on the screen
Every data stream has a numeric id:
Let’s explain redirection a bit more with detail:
How to redirect Standard output and Standard error in Bash:
To redirect the standard output of the command, we will use “1” with a redirection operator that is greater than the “>” sign:
The above command will create a file and place the standard output of the “ls” command in the “stdout.txt” file.
To read the “stdout.txt” file, use:
We can redirect standard error to a file as well by using the command:
To view the “stderr.txt” file, use:
Make sure use “2” will greater than the “>” sign. Since there is no “myfile.txt” file in the directory, the “cat” command will give an error that will be appended in the “stderr.txt” file.
These standard outputs can be redirected with a single command also, use:
The output of the “ls” command will be written in the “stdout.txt” file, but the “stderr.txt” will remain empty because there would be no error.
Now let’s do for “stderr.txt”:
Use the below-mentioned command to read “stderr.txt.”
And of course, “stdout.txt” will be empty.
Linux command upon executing gives standard output that could be a success output or an error output. Generally, these outputs cannot be redirected using redirection operators; we need to use specific numeric ids with the “>” sign. In this guide, we learned how to use these numeric keys to redirect standard output to a file with examples.