Which of the following symbols could be used to redirect stdout?
Learn how to process the output of shell commands within a script and send it to files, devices, or other commands or scripts. Show
Posted: April 21, 2022 |%t min read| by Roberto Nozaki (Sudoer, Red Hat) Image Photo by Isaque Pereira from Pexels Shell scripts provide a very powerful feature: the ability to redirect the output from commands and scripts and send it to files, devices, or even as input to other commands or scripts. Training & certification
This article focuses on command and script output. Types of outputCommands and scripts in a shell can generate two basic types of outputs:
By default, STDOUT and STDERR are sent to your terminal's screen. In terms of input, STDIN by default reads input from the keyboard (file descriptor 0). A file descriptor is a unique identifier for a file or other I/O resource. How to redirect shell outputThere are multiple ways to redirect output from shell scripts and commands. 1. Redirect STDOUTFor the following examples, I will use this simple set of files:
I'm running a simple I can redirect the standard output to a file using 0:
Next, I run a similar command, but with a 1 before 2. Redirecting using the 2 signal is the same as using 4 to do so: I'm telling the shell to redirect the STDOUT to that file. If I omit the file descriptor, STDOUT is used by default. I can prove this by running the 5 command to show the output of both commands side by side:
As you can see, both outputs have the same content. 2. Redirect STDERRNow, what is special about STDERR? To demonstrate, I will introduce an error condition to the previous example with 6:Here's the result: Image (Roberto Nozaki, CC BY-SA 4.0)Here are some observations from the above test:
[ Download a Bash Shell Scripting Cheat Sheet. ] Next, I'll redirect the error output by referencing file descriptor 2 explicitly with 7Image (Roberto Nozaki, CC BY-SA 4.0)In the example above:
3. Send STDOUT and STDERR to the same fileAnother common situation is to send both STDOUT and STDERR to the same file:
In this example, all output (normal and error) is sent to the same file. The 0 construction means "send the STDERR to the same place you are sending the STDOUT."Image Download now 4. Redirect output, but append the fileIn all the previous examples, whenever I redirected some output, I used a single 2, which means "send something to this file, and start the file from scratch." As a result, if the destination file exists, it is overwritten.If I want to append to an existing file, I need to use 2. If the file doesn't already exist, it will be created:
5. Redirect to another process or to nowhereThe examples above cover redirecting output to a file, but you can also redirect outputs to other processes or 3.Sending output to other processes is one of the most powerful features of a shell. For this task, use the 4 (pipe) symbol, which sends the output from one command to the input of the next command:
The above example lists my processes, filters any that contain the string chrome, ignores the line about my 5 command, and counts the resulting lines. If I want to send the output to a file, I add 2 and a file name to the end of the chain.Finally, here is an example where I want to ignore one of the outputs, the STDERR:
Because the 7 command did not find any files with names starting with 8, some error messages display at the end.Suppose I create some scripts and don't care about seeing or capturing these errors (I know, in real life, you should prevent and handle the errors, not just ignore them):
The 9 is a special device file that is like a "black hole": What you send there just disappears.[ Download this guide to installing applications on Linux. ] 6. Use redirection in a scriptImage (Roberto Nozaki, CC BY-SA 4.0)
More Linux resources
This very simple script does the following:
Wrap upThose were some examples of redirecting STDOUT and STDERR. Putting all this together, you realize how powerful redirection can be. By chaining individual commands, manipulating their output, and using the result as the input for the next command, you can perform tasks that otherwise could require you to develop a script or program. You could also incorporate the technique into other scripts, using everything as building blocks. Check out these related articles on Enable SysadminImage How to access the Linux terminal You've installed Linux; now what? Here's how to access the console in text mode or with the GUI. Posted: April 12, 2022 Author: Nathan Lager (Sudoer, Red Hat) Image What sysadmins need to know about using Bash You've logged into Linux, now what? Here's how to use Bash, the command-line interpreter on most modern Linux machines. Posted: April 14, 2022 Author: Nathan Lager (Sudoer, Red Hat) Image How to use inputs in your shell scripts Use variables and arguments to change, improve, and reuse your favorite shell scripts. Posted: April 19, 2022 Author: Roberto Nozaki (Sudoer, Red Hat) Topics: Certification Linux Red Hat Enterprise Linux (RHEL) Career Roberto NozakiRoberto Nozaki (RHCSA/RHCE/RHCA) is an Automation Principal Consultant at Red Hat Canada where he specializes in IT automation with Ansible. More about me Red Hat Summit 2022: On DemandGet the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand. Register for free Related ContentImage How to schedule jobs using the Linux 'cron' utility Scheduling tasks to run automatically at specific times is essential knowledge for any sysadmin. Posted: December 15, 2022 Author: Alexon Oliveira (Sudoer, Red Hat) Image Learn how Unix influenced Linux, understand Ansible lists and dictionaries, and more tips for sysadmin Which symbol is used for redirect?Redirection is done using either the ">" (greater-than symbol), or using the "|" (pipe) operator which sends the standard output of one command to another command as standard input.
What is the meaning of 2 >& 1?The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").
Which symbol is used for standards output stdout redirection?Redirecting Standard Error and Other Output
Generally, when a command starts, three files are already open: stdin (standard input), stdout (standard output), and stderr (standard error). If you want to redirect standard input or standard output, you can use the <, >, or > > symbols.
Which of the following is not a redirection symbol A B C * D >>?* is not a redirection symbol. In general, a redirection operator is a special character that can be used with a command, like DOS command or a Command Prompt command to either redirect the input to the command as well as the output from the command. Command inputs as well as outputs are called command handles.
|