15.11.2023

How to use redirect operators in Bash?

Introduction

How to redirect output or input of files, command or process in the Linux system? For that question in the instruction we will give answer! That can be useful for automatization extraordinary and quite commonly system, by make script, program or another stuff. Below you can get like briefly and thoroughly theory answer!

Short theory

For run any program in the Linux, Windows and other operating system, we just need to click on the icon or executable file. But core of OS in that moment create process. Process - it's passive and abstract entity, which describe resources and location of running program. Therefore if process passive, then there is active entity - thread.

Each of processes have 3 standart thread: input, output and error. They are fundamental part of any system. If we want to customize usage, for redirect thread, we need to call and indicate them in the CLI. That can be possible with file descriptors, which represent number that assign to thread, file, network socket and any element of the operating system. Schema of logic that mechanism you can see below:

Screenshot №1 — Schema

In the result for redirect input, output or error we need to use command below:

cat 0< vocabulary.txt

Screenshot №2 — Example

For the cat command we didn't indicate any arguments for input, instead of this we write file descriptor for standart input 0< and give text. In the result we can see written text of the vocabulary.txt. The mechanism of file descriptors may looks like quite complicated system, therefore for build logical bundle, you can consider chapter below!

Files descriptors

Representing resources with descriptors allows different parts of a program to refer to the same underlying object without having to keep track of its location in memory, making it easier to manipulate and manage shared resources. Descriptors are simply integers that represent an open file or other input/output channel; they're called "descriptors" because you can think of them as pointers: rather than having to keep track of where a specific resource is stored in memory (which would be difficult if there were multiple copies), you simply need to know its descriptor value and use it whenever you want to access that resource.

Screenshot №3 — File descriptors

For example, if an application needs access to several files at once, each file can be represented by its own integer value: rather than having to keep track of where each file is stored in memory (which would be difficult if there were multiple copies), the program simply needs to know the descriptor value for each resource and use it whenever it wants to access them. Descriptors are commonly used for files, but they're not limited to them; any type of system resource can be represented by a single integer value: network sockets or other types of communication channels.

In summary, while descriptors are simply integers that represent an open file or other input/output channel, they're called "descriptors" because you can think of them as pointers: rather than having to keep track of where a specific resource is stored in memory (which would be difficult if there were multiple copies), the program simply needs to know its descriptor value and use it whenever.

Basic operators

Alright, after pice of math part we can consider operators for redirection thread, they indicate as we discuss above as a sing's.

The standard output or redirection, form of usage: command > output_object. That customize direction of thread literally mean result of command don't displayed on the screen of the terminal, them redirect to the object. If file doesn't exist, they will create by system, but if file already exist it will be completely replaced.

For example, we have task for write all package with name "apache2" from our repository in the file, by one command or we need to use that in the script.

apt list | grep "apache2" > apache.txt

After entering command we need to see context:

tail apache.txt

Screenshot №4 — Redirection stdout

But if we want to append data to the existing file, for that purpose we will use next form:  command >> output. In the difference at the example above file doesn't replace, but in the case when file wasn't created, after entering command we can see them in the catalogue.

However, if we need to hide output, then we can use special device in the Linux, they named null. Their main function it's discard output thread of data, also we can redirect traffic to another device, by that way.

The standard error, form of usage: command 2> output_object. Commonly, use in the cases when we need to redirect only error's event among all log-file. That sing 2> also as we consider above means create or replace exist file. We consider case when we need to grep error's from started ls command and redirect to the file:

ls don't_exist_dir 2> err0rs.txt

Screenshot №5 — Redirection error's

If we want to add some information without replace, then use already a familiar operator 2>> which create or append data to the file. For example:

apt-get errr 2>> err0rs.txt

And look at the result of test operation:

cat err0rs.txt

Screenshot №6 — Error's append

As we can see above new line don't replace all file, then operators of redirect work properly!

There is situation when we need to use simultaneously both operators, when we intercept data from daemon or command, system can write standart output and error in the one file:

ls /tmp /jgj &> log.txt

Let' check the result of command:

cat log.txt

Screenshot №7 — Redirect into the one file

We indicate two catalogue and one of them give error and passed to the standart output, so that we can see on the screenshot.

For some situations, we need to build more complicated redirection. Imagine you need to sort object from file and write it to the new file:

sort < err0rs.txt > sort_err0rs.txt

Screenshot №8 — Complicated redirection

Let's explain, how that work! At the first execute stdinput from err0rs.txt to command sort and result of that operations redirect to the s0rt_err0rs.txt that's it.

You can notice, that we use operators for the redirection to the device or file but, what if we redirect to output from our command to input another command? Unfortunately, operators described above don't fit to the our criteria. For that purposes there is pipe. What is that mean? Literally pipe! Indicated by a sign | and use in form: command_1 | command_2:

apt list | grep "apache2"

Screenshot №9 — Pipe

Line of commands at the first gather data from apt command write to the stdoutput and redirect to the stdinput with out proxy-file! In the similar way by using standart operators that will looks like this:

apt list >> proxy_file.txt >> grep "apache2"

In that case we use proxy-file and save information to it!

Conclusion

The understanding how to use redirect operators in Bash is a fundamental skill for anyone working with the command line in Unix-like operating systems. These operators provide a powerful way to manipulate input and output streams, enabling you to efficiently handle data and processes. By mastering concepts such as <, >, >>, 2>, and &>, you can not only redirect data but also combine, append, and manage errors effectively.