{"id":17675,"date":"2023-08-27T07:43:33","date_gmt":"2023-08-27T07:43:33","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=17675"},"modified":"2024-01-16T08:18:09","modified_gmt":"2024-01-16T08:18:09","slug":"bash-redirection","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/","title":{"rendered":"Bash Redirection"},"content":{"rendered":"<p style=\"text-align: justify;\">In <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/introduction\/\" target=\"_blank\" rel=\"noopener\"><strong>Bash<\/strong><\/a>, the fundamental method for manipulating input and output is through <strong>redirection<\/strong>. <strong>Redirection <\/strong>allows you to alter the source or destination of a program\u2019s file descriptors. It is a process of manipulating the input and output of commands in <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/introduction\/what-is-bash-scripting\/\" target=\"_blank\" rel=\"noopener\"><strong>Bash scripting<\/strong><\/a>. From this writing, you will learn the basics, types &amp; order of Bash redirection.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#What_is_Redirection\" >What is Redirection?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Standard_Streams\" >Standard Streams<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#1_Standard_Input_stdin\" >1. Standard Input (stdin)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#2_Standard_Output_stdout\" >2. Standard Output (stdout)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#3_Standard_Error_stderr\" >3. Standard Error (stderr)<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Bash_Redirection_Operators\" >Bash Redirection Operators<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Various_Cases_of_Bash_Redirection\" >Various Cases of Bash Redirection<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#A_Bash_Input_Redirection\" >A. Bash Input Redirection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#B_Bash_Output_Redirection\" >B. Bash Output Redirection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#C_Appending_Redirected_Output\" >C. Appending Redirected Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#D_Bash_Standard_Error_Redirection\" >D. Bash Standard Error Redirection<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Bash_Echo_to_Stderr\" >Bash Echo to Stderr<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Bash_Stderr_to_Stdout\" >Bash Stderr to Stdout<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#E_Redirecting_Standard_Output_and_Standard_Error\" >E. Redirecting Standard Output and Standard Error<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#F_Appending_Standard_Output_and_Standard_Error\" >F. Appending Standard Output and Standard Error<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Summarized_Table_for_Redirection_Syntaxes_in_Bash\" >Summarized Table for Redirection Syntaxes in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Order_of_Redirection_%E2%80%9C%3Efile_2%3E_1%E2%80%9D_vs_%E2%80%9C2%3E_1%3Efile%E2%80%9D\" >Order of Redirection: \u201c&gt;file 2&gt;&amp;1\u201d vs. \u201c2&gt;&amp;1&gt;file\u201d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Few_Tips_on_Effective_Redirection\" >Few Tips on Effective Redirection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#People_Also_Ask\" >People Also Ask<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Redirection\"><\/span>What is Redirection?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxsimply.com\/what-is-redirection-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>Redirection<\/strong><\/a> is a process that allows you to change the default input source or output destination of a command. With <strong>redirection<\/strong>, you can redirect output to a file instead of displaying it on the terminal, or instruct an application to read input from a file rather than the keyboard. <strong>Redirections <\/strong>are executed by the <a href=\"https:\/\/linuxsimply.com\/what-is-a-shell-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>Bash shell<\/strong><\/a> before running the command to which the redirections are applied.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Standard_Streams\"><\/span>Standard Streams<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Standard streams<\/strong> are redirected to files or from files in <strong>Linux<\/strong>. They are basically the ways of exchanging data between the programs and their environments. There are<strong> three<\/strong> types of standard streams, <strong>standard input, output <\/strong>&amp; <strong>error. <\/strong>Through them, you get to control your script, like, from where it will read input, where its output will be destined, and how it handles error messages.<\/p>\n<p style=\"text-align: justify;\">Now, in <strong>Linux<\/strong>, everything is treated as a file, and input and output are no exception. The <strong>OS <\/strong>assigns identifiers called <strong>file descriptors<\/strong> to represent these files. Each process can have a maximum of <strong>nine<\/strong> open <strong>file descriptors<\/strong>. In the bash shell, the first <strong>three<\/strong> descriptors are reserved with specific IDs for these standard streams. They are,<\/p>\n<ul style=\"text-align: justify;\">\n<li><strong>0<\/strong> (represents <strong>STDIN<\/strong>)<\/li>\n<li><strong>1<\/strong> (represents <strong>STDOUT<\/strong>)<\/li>\n<li><strong>2 <\/strong>(represents <strong>STDERR<\/strong>)<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Check out the following graphical presentation of<strong> STDIN, STDOUT,<\/strong> and <strong>STDERR<\/strong> with a more detailed description for a clearer concept: <img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-17709\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/stdin-stdout-_-stderr.png\" alt=\"Relationship between bash stdin, stdout &amp; stderr.\" width=\"825\" height=\"246\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/stdin-stdout-_-stderr.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/stdin-stdout-_-stderr-300x89.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/stdin-stdout-_-stderr-768x229.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"1_Standard_Input_stdin\"><\/span>1. Standard Input (stdin)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><strong>Bash <\/strong>provides a way to interactively read input from the user or another command using standard input which allows scripts and programs to accept data directly from the keyboard or other input sources. You can <strong>read from stdin <\/strong>using the<strong> \u2018read\u2019 <\/strong>command, which allows you to assign input to variables for further processing in the script.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"2_Standard_Output_stdout\"><\/span>2. Standard Output (stdout)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><strong>Bash<\/strong> allows you to display output using standard output. By default, the output is displayed on the terminal. But you can also <strong>redirect<\/strong> the output to a file using the <strong>\u2018&gt;\u2019<\/strong> symbol or <strong>append <\/strong>it to a file using the <strong>\u2018&gt;&gt;\u2019<\/strong> symbol. Moreover, you can also pipe the output of one command to another using the <strong>\u2018|\u2019<\/strong> symbol.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"3_Standard_Error_stderr\"><\/span>3. Standard Error (stderr)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">In addition to <strong>STDOUT<\/strong>, bash also provides a separate stream called<strong> standard error<\/strong> for handling error messages &amp; diagnostic output. By default,<strong> STDERR <\/strong>is displayed on the terminal along with <strong>STDOUT<\/strong>. You can redirect <strong>STDERR<\/strong> to a file also using the <strong>\u20182&gt;\u2019<\/strong> or append using the <strong>\u20182&gt;&gt;\u2019<\/strong> symbol.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Bash_Redirection_Operators\"><\/span>Bash Redirection Operators<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Check out the following list of <strong>redirection operators <\/strong>normally used in performing redirection operations in <strong>Bash<\/strong>:<\/p>\n<p style=\"text-align: left;\"><div class=\"su-list\" style=\"margin-left:8px\">\n<ul>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&gt;<\/strong>: Redirects standard output (stdout) to a file. If the file already exists, its contents will be overwritten.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&gt;&gt;<\/strong>: Redirects stdout to a file but appends the output to the end of the file if it already exists.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&lt;<\/strong>: Redirects standard input (stdin) to a command from a file. The command will read input from the specified file.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>2&gt;<\/strong>: Redirects standard error (stderr) to a file. Error messages produced by the command will be written to the specified file.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>2&gt;&gt;<\/strong>: Redirects stderr to file, but appends the error messages to the end of the file if it already exists.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&amp;&gt;<\/strong> or <strong>&gt;&amp;<\/strong>: Redirects both stdout and stderr to the same location. This operator is equivalent to using both \u2018&gt;file\u2019 and \u20182&gt;file\u2019.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&gt;&gt;&amp;<\/strong> or <strong>&amp;&gt;&gt;<\/strong>: Redirects both stdout &amp; stderr to the same file, but appends the output to the end of the file if it already exists.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&lt;&lt;<\/strong>: Represents \u2018Here Document\u2019, which allows you to provide input to a command within the script without needing an external file.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&lt;&lt;&lt;<\/strong>: Represents \u2018Here String\u2019, which allows you to provide a string as the input to a command.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>&gt;|<\/strong>: Forces redirection to a file, even if the noclobber option (prevents accidental overwriting of existing files) is set.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>2&gt;&amp;<\/strong>-: Tells Bash to close or discard the stderr stream in a Bash shell.<\/li>\n<li><i class=\"sui sui-arrow-right\" style=\"color:#000000\"><\/i> <strong>|<\/strong>: Pipe connects the stdout of one command to the stdin of another command, creating a data processing pipeline.<\/div><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Various_Cases_of_Bash_Redirection\"><\/span>Various Cases of Bash Redirection<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In the following writing, I will discuss various ways, you can redirect stdin, stdout &amp; stderr of commands for your bash scripts. So, check out these cases of redirection available in <strong>Linux<\/strong>:<\/p>\n<p style=\"text-align: justify;\"><div class=\"su-note\"  style=\"border-color:#e5e3d9;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#fffdf3;border-color:#ffffff;color:#333333;\">\n<p style=\"text-align: justify;\">You can view the <a href=\"#Summarized_Table\"><strong>Summarized Table<\/strong><\/a> of the command syntaxes that you can use for redirection purposes. <\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"A_Bash_Input_Redirection\"><\/span>A. Bash Input Redirection<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Redirecting input is a process where the <strong>standard input (stdin)<\/strong> of a command is changed from the keyboard to a\u00a0 file. By, using the<strong> \u2018&lt;\u2019 <\/strong>symbol you can take input data from a file instead of typing it interactively. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">cat &lt; file.txt<\/code><\/pre>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-17702\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/1.-stdin-redirection.png\" alt=\"Bash input redirection\" width=\"700\" height=\"182\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/1.-stdin-redirection.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/1.-stdin-redirection-300x78.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/1.-stdin-redirection-765x199.png 765w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The input used for the \u2018<a href=\"https:\/\/linuxsimply.com\/cat-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>cat command<\/strong><\/a>\u2019 here is redirected from the \u2018<strong>file.txt<\/strong>\u2019 file.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"B_Bash_Output_Redirection\"><\/span>B. Bash Output Redirection<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Here, the displaying of the <strong>standard output (stdout)<\/strong> of a command is changed from the terminal screen to a file or another command\u2019s input. The<strong> \u2018&gt;\u2019<\/strong> symbol is used for this purpose. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">whoami &gt; output.txt<\/code><\/pre>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-17703\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/2.-stdout-redirection.png\" alt=\"Bash output redirection\" width=\"700\" height=\"132\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/2.-stdout-redirection.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/2.-stdout-redirection-300x57.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/2.-stdout-redirection-764x144.png 764w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The output of the <strong>\u2018<\/strong><a href=\"https:\/\/linuxsimply.com\/whoami-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>whoami command<\/strong><\/a><strong>\u2019<\/strong> is redirected to the \u2018<strong>output.txt<\/strong>\u2019 file. Which I viewed using the \u2018<strong>cat command<\/strong>\u2019.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"C_Appending_Redirected_Output\"><\/span>C. Appending Redirected Output<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Normally, when you redirect a stdout to a file with the<strong> \u2018&gt;\u2019 <\/strong>redirection operator, it overwrites the file. You can also append the stdout to the existing file contents while redirecting by using the <strong>\u2018&gt;&gt;\u2019 <\/strong>redirection operators. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">whoami &gt;&gt; output.txt<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17704\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/3.-appending-stdout.png\" alt=\"Bash appending output\" width=\"825\" height=\"311\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/3.-appending-stdout.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/3.-appending-stdout-300x113.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/3.-appending-stdout-767x289.png 767w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>Here, I used the <strong>append operator (&gt;&gt;)<\/strong>, and as a result,\u00a0 instead of overwriting it, the command output is appended to the <strong>\u2018output.txt\u2019<\/strong> file.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"D_Bash_Standard_Error_Redirection\"><\/span>D. Bash Standard Error Redirection<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>standard error<\/strong> <strong>(stderr)<\/strong> is a way to control where error messages, generated by commands are sent. You can redirect stderr to a file, discard it, or merge it with the standard output. You can use the redirection operator <strong>\u201c2&gt;\u201d<\/strong> to <strong>redirect<\/strong> the <strong>standard error<\/strong> of a command to a file, \u201c2&gt;&gt;\u201d to append, or \u201c2&gt; \/dev\/null\u201d to discard it. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">whoami -l 2&gt; output.txt<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17705\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/4.-stderr-redirection.png\" alt=\"Bash stderr redirection\" width=\"825\" height=\"188\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/4.-stderr-redirection.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/4.-stderr-redirection-300x68.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/4.-stderr-redirection-768x175.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>From the image, you can see I used the wrong option for the command <strong>\u2018whoami\u2019<\/strong>. As a result, the command output shows an error message. However, it was not displayed on the screen, as was redirected to the <strong>\u2018output.txt\u2019<\/strong> file. However, I viewed it later using the<strong> \u2018cat command\u2019<\/strong>.<\/p>\n<p style=\"text-align: justify;\">Now, let\u2019s discuss two special cases of<strong> stderr<\/strong>:<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Bash_Echo_to_Stderr\"><\/span>Bash Echo to Stderr<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">Here, the output of the <a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a> will be redirected to the <strong>stderr<\/strong>. And for that, the operator<strong> \u2018&gt;&amp;2\u2019<\/strong> is used. Where<strong> \u20182\u2019<\/strong> represents the file descriptor for<strong> stderr<\/strong>. So, <strong>\u2018&gt;&amp;2\u2019 <\/strong>redirects the command output to the standard error stream. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo \u201cThis is an error message!\u201d &gt;&amp;2<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Bash_Stderr_to_Stdout\"><\/span>Bash Stderr to Stdout<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">In this case, the <strong>stderr<\/strong> will be redirected to the<strong> stdout<\/strong>. For that, you can use the operator <strong>\u20182&gt;&amp;1\u2019<\/strong>. This will cause both <strong>stderr <\/strong>and <strong>stdout<\/strong> to be combined and directed to the same location. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">command &gt; file 2&gt;&amp;1<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"E_Redirecting_Standard_Output_and_Standard_Error\"><\/span>E. Redirecting Standard Output and Standard Error<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In <strong>Linux<\/strong>, every process has two default output streams, <strong>stdout <\/strong>(represented by <strong>file descriptor 1<\/strong>) &amp; <strong>standard error<\/strong> or <strong>stderr <\/strong>(represented by<strong> file descriptor 2<\/strong>). You can redirect both of them to the same file using the symbol <strong>\u2018&amp;&gt;<\/strong> or<strong> \u2018&gt;&amp;\u2019<\/strong> or<strong> \u20182&gt;&amp;1\u2019<\/strong>. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">ls \/nonexistent_directory &amp;&gt; log.txt<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17706\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/5.-redirecting-stdout-_-stderr.png\" alt=\"Bash stdout &amp; stderr redirection\" width=\"825\" height=\"127\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/5.-redirecting-stdout-_-stderr.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/5.-redirecting-stdout-_-stderr-300x46.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/5.-redirecting-stdout-_-stderr-767x118.png 767w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The output &amp; output error messages both are redirected to the \u2018log.txt\u2019 file without being displayed on the screen. Later I viewed the \u2018log.txt\u2019 file using the cat command.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"F_Appending_Standard_Output_and_Standard_Error\"><\/span>F. Appending Standard Output and Standard Error<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In the same way as appending stdout, you can append both <strong>stdout<\/strong> &amp;<strong> stderr <\/strong>at the same time to the same file too. For that, use the append symbol<strong> \u2018&gt;&gt;\u2019<\/strong>. For example,<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">ls \/nonexistent_directory &gt;&gt; output.txt 2&gt;&amp;1<\/code><\/pre>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17707\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/6.-appending-stdin-_-stderr.png\" alt=\"Bash stdout &amp; stderr appending\" width=\"825\" height=\"199\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/6.-appending-stdin-_-stderr.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/6.-appending-stdin-_-stderr-300x72.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/6.-appending-stdin-_-stderr-767x185.png 767w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>Here, I used the <strong>append operator (&gt;&gt;) <\/strong>to append both<strong> stdout<\/strong> &amp;<strong> stderr,<\/strong> and as a result,\u00a0 instead of overwriting it, the command output with the error message is appended to the <strong>\u2018log.txt\u2019<\/strong> file. And later I viewed the <strong>log.txt<\/strong> file using the <strong>cat command<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Summarized_Table_for_Redirection_Syntaxes_in_Bash\"><\/span><a id=\"Summarized_Table\"><\/a>Summarized Table for Redirection Syntaxes in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In the following table, I tried to summarize all the cases you may need while performing redirection with their generalized command syntaxes:<\/p>\n<div class=\"su-table su-table-responsive su-table-alternate su-table-fixed\">\n<table style=\"width: 100%\" width=\"100%\">\n<tbody>\n<tr>\n<th style=\"width: 50.9892%\" width=\"300\"><strong>Cases<\/strong><\/th>\n<th style=\"width: 50.4281%\" width=\"322\"><strong>Command Syntaxes<\/strong><\/th>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Redirect input<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command &lt; file.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Redirect Output<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command &gt; output.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Append Output<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command &gt;&gt; output.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Redirect Standard Error<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command 2&gt; error.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Append Standard Error<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command 2&gt;&gt; error.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Redirect stdout &amp; stderr to the same file<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command &amp;&gt; file.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Redirect stdout &amp; stderr to different files<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command &gt;output.txt\u00a0 2&gt;error.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Append stdout &amp; stderr to the same file<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command &amp;&gt;&gt; output.txt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Suppressing stderr<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command 2&gt;&amp;-<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50.9892%\" width=\"300\"><strong>Redirect stderr to stdout<\/strong><\/td>\n<td style=\"width: 50.4281%\" width=\"322\">command 1&gt;&amp;2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Order_of_Redirection_%E2%80%9C%3Efile_2%3E_1%E2%80%9D_vs_%E2%80%9C2%3E_1%3Efile%E2%80%9D\"><\/span>Order of Redirection: \u201c&gt;file 2&gt;&amp;1\u201d vs. \u201c2&gt;&amp;1&gt;file\u201d<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong>order of redirection <\/strong>in Bash commands matters, and it can result in different outcomes depending on where you place them. They are set up from left to right. For a clear understanding see the differences between the following command syntaxes:<\/p>\n<ul style=\"text-align: justify;\">\n<li><span style=\"color: #800080;\"><strong>command &gt; file 2&gt;&amp;1<\/strong><\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Here, the <strong>stdout<\/strong> is redirected to the <strong>\u2018file\u2019<\/strong>, and then the<strong> stderr<\/strong> is redirected to the same location as the <strong>stdout<\/strong>. This means both stdout and stderr will be sent to the file named<strong> \u2018file\u2019<\/strong>.<\/p>\n<ul style=\"text-align: justify;\">\n<li><span style=\"color: #800080;\"><strong>command 2&gt;&amp;1 &gt; file<\/strong><\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">In this case, the <strong>stderr i<\/strong>s redirected to the same location as the<strong> stdout<\/strong>, which is initially the <strong>terminal<\/strong>. Then, the stdout is redirected to the file<strong> \u2018file\u2019<\/strong>. This means <strong>stdout <\/strong>will go to the file <strong>\u2018file\u2019<\/strong>, but <strong>stderr <\/strong>will still go to the <strong>terminal<\/strong> because stderr\u2019s redirection happened before stdout\u2019s redirection.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Few_Tips_on_Effective_Redirection\"><\/span>Few Tips on Effective Redirection<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong>Bash shell <\/strong>maintains a flexible stance on what it defines as a valid redirection. So remember these points while performing redirection operations to avoid unnecessary errors:<\/p>\n<ul>\n<li style=\"text-align: justify;\">Maintain tight grouping of redirections. That means, avoiding adding spaces within redirection syntax except within quotes when necessary (e.g. for filenames with spaces).<\/li>\n<li style=\"text-align: justify;\">Use whitespace to separate redirections visually, creating chunks without unnecessary spaces.<\/li>\n<li style=\"text-align: justify;\">Place a space between each redirection and between the argument list and the initial redirection.<\/li>\n<li style=\"text-align: justify;\">Position redirections at the command\u2019s end, following all arguments.<\/li>\n<li style=\"text-align: justify;\">Do not start a command with a redirection or insert one in the middle of arguments.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To sum up, now that you know, using <strong>Bash<\/strong> <strong>redirection<\/strong> you can redirect input and output streams to or from files to perform complex operations and automate tasks. You can get an idea of how useful it can be while you are writing <strong>Bash scripts<\/strong>. In this writing, I tried to discuss the overall redirection basics you need to know. Hope it helps!<\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><div class=\"su-accordion su-u-trim\">\n<p style=\"text-align: justify;\"><div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>What is redirection in bash?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\"> <strong>Redirection<\/strong> is a process that allows you to change the default input source or output destination of a command. With <strong>redirection<\/strong>, you can redirect output to a file instead of displaying it on the terminal, or instruct an application to read input from a file rather than the keyboard.<\/div><\/div> <div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>How do I redirect a file in bash?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\"> Redirecting a file in <strong>Bash<\/strong> means sending the contents of a file as input to a command or process. You can do that by using the redirection operator <strong>\u2018&lt;\u2019<\/strong>. For that, use the command syntax<strong> \u2018command &lt; input.file<\/strong>\u2019.<\/div><\/div>\n<p style=\"text-align: justify;\"><div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>Why is redirection used?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\"> Redirection in <strong>Bash<\/strong> and other shell scripting languages is used to control the input and output of commands and processes. It allows you to manipulate where the data comes from or goes to.<\/div><\/div>\n<p style=\"text-align: justify;\"><div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span><strong>How do I use redirection in Linux?<\/strong><\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\"> In <strong>Linux<\/strong>, you can use redirection operators to control the input and output of commands and processes. You can perform input redirection for redirecting the contents of a file as input to a command with the operator<strong> \u2018&lt;\u2019<\/strong>. And, you can perform output redirection to redirect the output of a command to a file using the operator<strong> \u2018&gt;\u2019<\/strong>. <\/div><\/div>\n<p style=\"text-align: justify;\"><\/div>\n<p><span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/read-from-stdin\/\" target=\"_blank\" rel=\"noopener\">How to Read From Stdin in Bash? [4 Cases With Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/input-redirection\/\" target=\"_blank\" rel=\"noopener\">Bash Input Redirection [3 Practical Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/output-redirection\/\" target=\"_blank\" rel=\"noopener\">What is Output Redirection in Bash [With 4 Practical Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/redirect-stderr\/\" target=\"_blank\" rel=\"noopener\">How to Redirect Stderr in Bash [5 Practical Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/redirect-stdout-and-stderr-to-file\/\" target=\"_blank\" rel=\"noopener\">How to Redirect Stdout and Stderr to File in Bash [5 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/stderr-to-stdout\/\" target=\"_blank\" rel=\"noopener\">Redirect Stderr to Stdout in Bash [5 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/echo-to-stderr\/\" target=\"_blank\" rel=\"noopener\">5 Ways to Echo to Stderr in Bash<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Redirection and Piping&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/\" target=\"_blank\" rel=\"noopener\">Bash Redirection and Piping<\/a><\/span> | <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;17675&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Bash Redirection&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            5\/5 - (1 vote)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Bash, the fundamental method for manipulating input and output is through redirection. Redirection allows you to alter the source &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Bash Redirection\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#more-17675\" aria-label=\"Read more about Bash Redirection\">Read more<\/a><\/p>\n","protected":false},"author":314897,"featured_media":17708,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[151],"class_list":["post-17675","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-bash-redirection-and-piping","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bash Redirection - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Redirection in Bash and other shell scripting languages is used to control the input and output of commands and processes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bash Redirection - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Redirection in Bash and other shell scripting languages is used to control the input and output of commands and processes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/moniramunny05\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-27T07:43:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-16T08:18:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Bash-redirection.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Monira Akter Munny\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Monira Akter Munny\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/\"},\"author\":{\"name\":\"Monira Akter Munny\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/2d8ccee92571d892b55ef5999761fc9e\"},\"headline\":\"Bash Redirection\",\"datePublished\":\"2023-08-27T07:43:33+00:00\",\"dateModified\":\"2024-01-16T08:18:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/\"},\"wordCount\":2132,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Bash-redirection.png\",\"keywords\":[\"bash redirection and piping\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/\",\"name\":\"Bash Redirection - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Bash-redirection.png\",\"datePublished\":\"2023-08-27T07:43:33+00:00\",\"dateModified\":\"2024-01-16T08:18:09+00:00\",\"description\":\"Redirection in Bash and other shell scripting languages is used to control the input and output of commands and processes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Bash-redirection.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/Bash-redirection.png\",\"width\":400,\"height\":400,\"caption\":\"Bash redirection\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/redirection\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bash Redirection and Piping\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/redirection-and-piping\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Bash Redirection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/2d8ccee92571d892b55ef5999761fc9e\",\"name\":\"Monira Akter Munny\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Monira-Akter-Munny-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Monira-Akter-Munny-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Monira-Akter-Munny-II-96x96.png\",\"caption\":\"Monira Akter Munny\"},\"description\":\"Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering &amp; Technology in the Electrical &amp; Electronics department. I'm more of an online gaming person who also loves to read blogs &amp; write. As an open-minded person ready to learn &amp; adapt to new territory, I'm always excited to explore the Linux world &amp; share it with you! Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/moniramunny05\",\"https:\\\/\\\/www.instagram.com\\\/monira_munny\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/monira-akter-munny-a76742113\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/munny\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bash Redirection - LinuxSimply","description":"Redirection in Bash and other shell scripting languages is used to control the input and output of commands and processes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/","og_locale":"en_US","og_type":"article","og_title":"Bash Redirection - LinuxSimply","og_description":"Redirection in Bash and other shell scripting languages is used to control the input and output of commands and processes.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/moniramunny05","article_published_time":"2023-08-27T07:43:33+00:00","article_modified_time":"2024-01-16T08:18:09+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Bash-redirection.png","type":"image\/png"}],"author":"Monira Akter Munny","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Monira Akter Munny","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/"},"author":{"name":"Monira Akter Munny","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/2d8ccee92571d892b55ef5999761fc9e"},"headline":"Bash Redirection","datePublished":"2023-08-27T07:43:33+00:00","dateModified":"2024-01-16T08:18:09+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/"},"wordCount":2132,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Bash-redirection.png","keywords":["bash redirection and piping"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/","name":"Bash Redirection - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Bash-redirection.png","datePublished":"2023-08-27T07:43:33+00:00","dateModified":"2024-01-16T08:18:09+00:00","description":"Redirection in Bash and other shell scripting languages is used to control the input and output of commands and processes.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Bash-redirection.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/08\/Bash-redirection.png","width":400,"height":400,"caption":"Bash redirection"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Bash Redirection and Piping","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/"},{"@type":"ListItem","position":4,"name":"Bash Redirection"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/2d8ccee92571d892b55ef5999761fc9e","name":"Monira Akter Munny","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Monira-Akter-Munny-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Monira-Akter-Munny-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Monira-Akter-Munny-II-96x96.png","caption":"Monira Akter Munny"},"description":"Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering &amp; Technology in the Electrical &amp; Electronics department. I'm more of an online gaming person who also loves to read blogs &amp; write. As an open-minded person ready to learn &amp; adapt to new territory, I'm always excited to explore the Linux world &amp; share it with you! Read Full Bio","sameAs":["https:\/\/www.facebook.com\/moniramunny05","https:\/\/www.instagram.com\/monira_munny\/","https:\/\/www.linkedin.com\/in\/monira-akter-munny-a76742113\/"],"url":"https:\/\/linuxsimply.com\/author\/munny\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/17675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314897"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=17675"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/17675\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/17708"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=17675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=17675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=17675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}