Error stream operating in the example

Stderr is very similar to stdout: with only one difference – it will contain an error report if it will take a place

script body:

#!/usr/bin/env bash

ls > /tmp/log
cat /tmp/log

ls /not/existed_path > /tmp/log
cat /tmp/log
cat /tmp/log

rm /tmp/log

The script running at the console:

script body in the yellow frame, output next

At this script, we make a listing of files in the current directory (“ls”) and redirect standard output to the file (“/tmp/log”). Then we display this file data at the console from the standard output stream of the cat utility that read the file. Next, we try to list files in the not existing folder and store results into the file as above, and display it two times. But there is no effect. Just one output about the error in the console. That is because this error comes from the standard errors stream of ls. We removed the file to clear some space “/tmp/log“.

Shell Scripting – Standard Input, Output and Error

Working on Linux applications we have several ways to get information from outside and to put it inside: command line args, environment variables, files. All of these sources are legal and good. But it has a finite size. Another way to establish communication is standard streams: input stream (stdin) used for getting data from outside of the app, output stream (stdout) to put data outside of the app, and error to put data outside of the app (stderr).

standard Linux application stream at sh

Each stream acts like a pipe: It has the same buffer to write and read the data. This buffer is available for reading from one application and available for writing from another one. On reading, the occupied buffer size will be reduced and it will be increased on writing. If the average rate of reading and writing is equal – then data passed over the stream can be any number of bytes long.

Table of Content

  • Input/output streams operating in the example
  • Error stream operating in the example
  • Streams redirecting
  • Discard the output
  • Pipelined streams
  • Here document
  • Shell Scripting – Standard Input, Output and Error – FAQs

Similar Reads

Input/output streams operating in the example

Let’s look at an example of the “grep” application describing how it interacts with it....

Error stream operating in the example

Stderr is very similar to stdout: with only one difference – it will contain an error report if it will take a place...

Streams redirecting

It is possible to manipulate the standard streams: redirect them or use pipes to process them....

Discard the output

In some cases, it is useful to drop all of the application output (to save storage space or remove the unimportant sources of data to analyze). We should remember that we should mute not just the standard output stream but the standard error stream also. Let’s look at an example of how does it work....

Pipelined streams

It also can be useful to modify the stream.  is able to run several applications connected by the streams....

Here document

Here the document is redirection option to fill input stream of application by information native way:...

Shell Scripting – Standard Input, Output and Error – FAQs

How can I redirect the output of a shell script to a file?...

Conclusion

There are several standard utilities to work with streams (cat, cut, grep, sed, tr, …). You can use it or write your own to make a data stream processing pipeline that will match your needs. Under the hood, it will work with stdin, stdout, stderr....

Contact Us