11.12.2024

An Input-Output redirections in Linux

Description

Linux is “built” around the philosophy “one tool for one task”. Due to this, mainstream Linux distros has much and much utilities, each of them has just a few functions but this functions are working great.

These utilities can send it’s “job” results from one process to another via input-output redirections mechanism. It could be useful in developing, services support etc.

All processes in Linux are working across 3 streams, standard input (stdin), standard output (stdout) and standard error (stderr).  These streams are having system-reserved numbers. Stdin stream has number “0”, stream “1” is used for standard output, stream “2” is uses for an errors showing.

“Input” stream receives data from the computer keyboard or from another script output. Some examples will be described further.

By default, standard output stream sends data to the system terminal too.

Standard error stream also sends its data to the system terminal, but one difference with previous point – standard output stream shows result if command executed successfully. In case something wrong – data will be send to the standard error output.

Streaming redirections

Described in previous paragraph system streams could redirects work results to each other, to variables, files etc. To 'forward" system streams bracket symbols are reserved.

Double brackets is "add" stream data to the end of “target” object’s exist content.

Single bracket will "replace" target object's content with “source” stream's data.

Pipes

“Pipe” symbol is reserved in Linux for "sending" stream from one utility to the file or another program. It seems like “program BEFORE pipe is working, then sends results to the input of utility AFTER pipe”. E.g. command history | grep touch will show user's command history to the console and “forward” it further. Then utility “grep” will filter this flow and show commands which contains word “touch” only.

Filter utilities

Filters are the class of system utilities which usually uses “together” with pipe. The most popular filters are:

Final conclusions

In summary, there are possible to say that system streams “management” is very useful skill. It would be fine for software development, server tech support or any automation scripts building.