Working with standard console outputs

In this topic, I am going to show you how to work with console output. You will learn:

  • how to send the output of one command to another;
  • redirect stderr to stdout;
  • suppress output

When you run a console app in a shell you may see output any output. Without any additional commands, we just see a lot of text in the console so it hardly can be analyzed.

Redirecting the output of a command to a file or piping it to another command, you might notice that the error messages are printed on the screen.

In Linux shells (Bash and others) and Windows command line, when a program is executed, it uses three standard I/O streams. Each stream is represented by a numeric file descriptor:
0 – stdin, the standard input stream.
1 – stdout, the standard output stream.
2 – stderr, the standard error stream.

The input stream provides information to the program, generally by typing on the keyboard. This means if you want to tell the program some external information you may use this stream and send some text from the keyboard or even load data from the text file.

The program output goes to the standard input stream and the error messages go to the standard error stream. Obviously, the standard error stream uses a program or shell built-in command to throw an error. All other information goes to the standard output stream. By default, both input and error streams are printed on the screen.

Redirecting Output
Redirection is a way to capture the output from a program or shell built-in command and send it as input to another program or file.

For output redirection to another program uses | (vertical bar). You can write several commands together to redirect each output to the next command. Example:

command1 | command2

or more complex:

command1 | command2 | command3 | command4 | command5

Streams can be redirected using the n> operator, where n is the file descriptor number (0 or 1 or 2).
When n is omitted, it defaults to 1, the standard output stream.c For example, the following two commands are the same; both will redirect the command output (stdout) to the file.

command > file

or

command 1> file

To redirect the standard error (stderr) use the 2> operator:

command 2> file

You can write both stderr and stdout to two separate files:

command 2> error.txt 1> output.txt



Redirecting stderr to stdout
When saving the program’s output to a file, usually stderr redirects to stdout so that you can have everything in a single file.
To redirect stderr to stdout to the same file, use the following:

command > file 2>&1

The order of redirection is important. In the following example only stdout redirects to the file. This happens because the stderr is redirected to stdout before the stdout was redirected anywhere.

command 2>&1 > file

The stdout redirects to a file, and 2>&1 redirect the stderr to the current location of stdout. As result both outputs will be in the same file.

Suppress output
To suppress the error messages from being displayed on the screen, redirect stderr to the null:

#This command for Linux
command 2> /dev/null
#This command for Windows
command 2> nul

To suppress all output uses:

#This command for Linux
command > /dev/null 2>&1

#This command for Windows
command 2> nul 2>&1

Additional links:
https://www.gnu.org/software/bash/manual/html_node/Redirections.html
https://learn.microsoft.com/en-us/troubleshoot/developer/visualstudio/cpp/language-compilers/redirecting-error-command-prompt

👇👇👇
💬Discuss or ask a question in the Telegram💬


by

Tags: