{"id":79072,"date":"2026-11-17T01:52:40","date_gmt":"2026-11-17T01:52:40","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=79072"},"modified":"2026-11-17T01:52:40","modified_gmt":"2026-11-17T01:52:40","slug":"linux-basics-04-streams","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=79072","title":{"rendered":"Linux Basics 04 \u2014 Pipes, Redirection, and Exit Status"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1990\">Linux Basics category<\/a> \u00b7 Lesson 4 of 8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>AI disclosure<\/strong> \u2014 AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Learning goal<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Route input, normal output, and errors, then detect a command failure inside a pipeline.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Concepts and setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Standard input is descriptor 0, standard output is 1, and standard error is 2. A pipe connects the left command&#8217;s standard output to the right command&#8217;s input. The shell opens redirection files before executing the command, so redirecting a file onto itself with &gt; can destroy the input. Use a different output file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This adapts the site\u2019s who &gt; users and numbered-line examples to synthetic data so that every learner can reproduce them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create and filter the site&#8217;s users-style file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>lab=$(mktemp -d)\ncd &quot;$lab&quot; || exit\nprintf '%s\\n' 'line1' &gt; users\nprintf '%s\\n' 'line2' &gt;&gt; users\nwc -l &lt; users\ngrep 'line2' users | tee matches.txt\ncat matches.txt missing.txt &gt; combined.txt 2&gt; errors.txt\nprintf 'cat status: %s\\n' &quot;$?&quot;\ncat combined.txt\nhead -n 1 errors.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2\nline2\ncat status: 1\nline2\ncat: missing.txt: No such file or directory<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Real who output can be empty on some sessions. The missing-file error is deliberate and went to errors.txt. The command still wrote the contents of matches.txt before it failed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Compare pipeline status in two Bash subshells<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>( set +o pipefail; false | true; printf '%s\\n' &quot;$?&quot; )\n( set -o pipefail; false | true; printf '%s\\n' &quot;$?&quot; )<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without pipefail, the last command decides the pipeline status. With it, a nonzero component status can be observed. Save $? immediately if you need it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Questions only<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>What is the difference between &gt; and &gt;&gt;?<\/li>\n\n\n<li>Does | include standard error by default?<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>Capture regular and error output of ls -d users missing in separate files.<\/li>\n\n\n<li>Explain the result of grep line2 users &gt; users.<\/li>\n\n\n<li>Predict the status of true | false with pipefail off.<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Questions and answers<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check answers<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>What is the difference between &gt; and &gt;&gt;?<\/strong><br><strong>Answer<\/strong> \u2014 The first truncates or creates its target. The second appends or creates it.<\/li>\n\n\n<li><strong>Does | include standard error by default?<\/strong><br><strong>Answer<\/strong> \u2014 No. It connects standard output only unless you redirect standard error too.<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment answer key<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>Capture regular and error output of ls -d users missing in separate files.<\/strong><br><strong>Answer<\/strong> \u2014 Use ls -d users missing &gt; seen.txt 2&gt; errors.txt and inspect both files.<\/li>\n\n\n<li><strong>Explain the result of grep line2 users &gt; users.<\/strong><br><strong>Answer<\/strong> \u2014 The shell truncates users before grep reads it. Write the result to a different file.<\/li>\n\n\n<li><strong>Predict the status of true | false with pipefail off.<\/strong><br><strong>Answer<\/strong> \u2014 1 because false is the last command.<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Source articles on this site<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=9643\">\u0987\u0989\u09a8\u09bf\u0995\u09cd\u09b8\u0983 \u09b8\u09c7\u09b2 \u0987\u09a8\u09aa\u09c1\u099f\/\u0986\u0989\u099f\u09aa\u09c1\u099f \u09b0\u09bf\u09a1\u09be\u0987\u09b0\u09c7\u0995\u09b6\u09a8 (Unix \u2013 Shell Input\/Output Redirections)<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=9594\">\u0987\u0989\u09a8\u09bf\u0995\u09cd\u09b8\u0983 \u09aa\u09be\u0987\u09aa \u098f\u09ac\u0982 \u09ab\u09bf\u09b2\u09cd\u099f\u09be\u09b0 (Unix \u2013 Pipes and Filters)<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=78655\">30 mixed quiz questions on Linux Command Line Skills<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Continue the course<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Previous lesson 03 \u2014 Editing Text and Reading Files. Next lesson 05 \u2014 Finding Files and Filtering Text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1990\">Linux Basics category<\/a> \u00b7 Lesson 4 of 8<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Route input, normal output, and errors, then detect a command failure inside a pipeline.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1990],"tags":[],"class_list":["post-79072","post","type-post","status-publish","format-standard","hentry","category-linux-basics","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":79102,"url":"http:\/\/bangla.sitestree.com\/?p=79102","url_meta":{"origin":79072,"position":0},"title":"Linux Administration Course \u2014 Start Here and Lesson Index","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"A 33-lesson Linux course from shell fundamentals to server administration, performance, and a recovery capstone.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79080,"url":"http:\/\/bangla.sitestree.com\/?p=79080","url_meta":{"origin":79072,"position":1},"title":"Linux Intermediate 04 \u2014 Bash Conditions, Loops, Functions, and Debugging","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Use a case statement, a function, and a loop to generate a multiplication chart and test invalid input.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79071,"url":"http:\/\/bangla.sitestree.com\/?p=79071","url_meta":{"origin":79072,"position":2},"title":"Linux Basics 03 \u2014 Editing Text and Reading Files","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Edit a text file in a terminal and verify the exact lines you saved.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79073,"url":"http:\/\/bangla.sitestree.com\/?p=79073","url_meta":{"origin":79072,"position":3},"title":"Linux Basics 05 \u2014 Finding Files and Filtering Text","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Choose find for names, grep for lines, cut for fields, and sed for transforming text.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79076,"url":"http:\/\/bangla.sitestree.com\/?p=79076","url_meta":{"origin":79072,"position":4},"title":"Linux Basics 08 \u2014 Archives, Compression, and a Restore Test","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Create and inspect an archive, then prove that you can recover a file from it.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79075,"url":"http:\/\/bangla.sitestree.com\/?p=79075","url_meta":{"origin":79072,"position":5},"title":"Linux Basics 07 \u2014 Processes, Jobs, and Signals","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Identify a process you own, inspect its state, and stop it gracefully.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79072"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79072\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79072"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}