Now, in this command, echo is our application. "hello" is our standard input, i.e., STDIN (0). Similarly, when we enter this command, meaning echo "hello", and press the Enter key, we get hello as our standard output, i.e., STDOUT (1).
Info
The echo command is basically used to print the given text or variables to the standard output (usually the screen).
Now let's touch upon our third standard, STDERR (2). For this, we need to enter a command that returns an error message. Let's look at the example below.
In this example, we used the ls -la hello.txt command to search for the hello.txt file in the directory. I don't actually have a file named hello.txt in my directory. That's precisely our goal – to get an error. As you can see, we received a standard error STDERR (2) like ls: cannot access 'hello.txt': No such file or directory. As you understand, this is a standard error.
Warning
This command ls -la hello.txt is used to list the detailed information (permissions, owner, size, last modified date, etc.) of the hello.txt file and does not use any standard input (STDIN). hello.txt is a file that this command processes, not standard input.
Now that we've learned the concepts of STDIN, STDOUT, and STDERR, let's move on to the redirection step to explain the 2>/dev/null concept.
Redirection is the process of changing the default sources (keyboard) or destinations (screen) of these standard streams. It's often used to connect files or other commands to these streams. It's done using special characters:
Takes a command's STDIN from a file instead of the keyboard.
It takes the input for the cat command from the text.txt file and writes its content to STDOUT (the screen). This does the same thing as cat text.txt but demonstrates how redirection works.
Redirects a command's STDOUT to a file instead of the screen.
Overwrite (>): If the target file exists, its content is deleted, and the command's output is written. If the file doesn't exist, it's created.
The output of the ls -la command is not written to the screen. Instead, it's written to the file named list.txt. If the file already exists, its old content is lost.
Append (>>): If the target file exists, the command's output is appended to the end of the file. If the file doesn't exist, it's created.
The output of the echo command (System stopped!) is appended to the end of the system.log file. The previous content of the file is preserved.
Redirects a command's STDERR (error messages) to a file instead of the screen. This is done using file descriptor 2.
As before, I don't have a file named hello.txt in my directory, so I will get an error, and this error will be written to the file I specified, not to the screen. I achieved this by redirecting STDERR (2). We use numbers when redirecting!
Sometimes you might want to redirect both normal output (STDOUT) and errors (STDERR) to the same file.
In this case, it redirects the compilation output, STDOUT (1), to build.log. It also redirects errors, STDERR (2), to where STDOUT (1) is currently being redirected (i.e., to build.log).
While not technically redirection, it's closely related to streams. It connects the STDOUT of one command to the STDIN of another command. It allows you to chain commands.
In this example, the files in the /etc directory are listed, and this list is written to STDOUT. Then, using |, the STDOUT of the ls command is taken and written as input (STDIN) to the grep tool. Subsequently, grep filters this input and writes its own STDOUT (to the screen).
We've thoroughly learned every detail up to this point. Now let's explain the 2>/dev/null concept. Of course, before that, we need to understand the /dev/null device.
What is the /dev/null device?
In short, it can be thought of as the "system trash can" or "bit bucket". Theoretically, it's an abstraction that allows data to be sent to nothingness. In summary, /dev/null is like a "virtual black hole where you throw data you want to discard".
Now let's break it down point by point;
command means it can be any command.
As we learned, STDERR (2) is being redirected using the > symbol.
The redirection target is set to /dev/null.
This means that when this redirection is used after any command, our errors (STDERR - 2) are redirected to /dev/null, i.e., to an irretrievable void (discarded).
Comments
Loading comments...