Only command after pipe executes, how does the pipe work in these scenarios? The 2019 Stack Overflow Developer Survey Results Are Inbash_history: comment out dangerous commands: `#`syslog-ng won't write to log file - “No such file or directory”How to avoid broken pipe in commands with cat?How to tell the 'bottleneck' in a muti-pipe commandPipe inside exec command won't workWhy isn't `|` treated literally in a glob pattern?When typing ctrl-c in a terminal, why isn't the foreground job terminated until it completes?Under what conditions exactly does SIGPIPE happen?How to write a function that reliably exits (with a specified status) the current process?How to quit command prompt after a sleeping command executes?

"as much details as you can remember"

Why are there uneven bright areas in this photo of black hole?

Can a flute soloist sit?

How much of the clove should I use when using big garlic heads?

Can you cast a spell on someone in the Ethereal Plane, if you are on the Material Plane and have the True Seeing spell active?

How to translate "being like"?

Output the Arecibo Message

Deal with toxic manager when you can't quit

Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?

How can I define good in a religion that claims no moral authority?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Are spiders unable to hurt humans, especially very small spiders?

Why can't devices on different VLANs, but on the same subnet, communicate?

What is the meaning of Triage in Cybersec world?

A female thief is not sold to make restitution -- so what happens instead?

Why isn't the circumferential light around the M87 black hole's event horizon symmetric?

If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?

Why couldn't they take pictures of a closer black hole?

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

Does HR tell a hiring manager about salary negotiations?

What information about me do stores get via my credit card?

Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?

Why doesn't shell automatically fix "useless use of cat"?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?



Only command after pipe executes, how does the pipe work in these scenarios?



The 2019 Stack Overflow Developer Survey Results Are Inbash_history: comment out dangerous commands: `#`syslog-ng won't write to log file - “No such file or directory”How to avoid broken pipe in commands with cat?How to tell the 'bottleneck' in a muti-pipe commandPipe inside exec command won't workWhy isn't `|` treated literally in a glob pattern?When typing ctrl-c in a terminal, why isn't the foreground job terminated until it completes?Under what conditions exactly does SIGPIPE happen?How to write a function that reliably exits (with a specified status) the current process?How to quit command prompt after a sleeping command executes?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?



example 1



$ cat /var/log/messages | date
Mon Apr 8 17:10:02 IST 2019


example 2



$ cat /var/log/messages | w
17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61









share|improve this question






























    -1















    What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?



    example 1



    $ cat /var/log/messages | date
    Mon Apr 8 17:10:02 IST 2019


    example 2



    $ cat /var/log/messages | w
    17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61









    share|improve this question


























      -1












      -1








      -1


      0






      What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?



      example 1



      $ cat /var/log/messages | date
      Mon Apr 8 17:10:02 IST 2019


      example 2



      $ cat /var/log/messages | w
      17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61









      share|improve this question
















      What happens in the following two examples? Why is only the second command in each pipeline executed? How does the pipe work here?



      example 1



      $ cat /var/log/messages | date
      Mon Apr 8 17:10:02 IST 2019


      example 2



      $ cat /var/log/messages | w
      17:10:34 up 8 days, 1:24, 6 users, load average: 0.63, 0.49, 0.61






      bash pipe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 8 at 12:23









      Kusalananda

      141k17263439




      141k17263439










      asked Apr 8 at 11:45









      supermansuperman

      145




      145




















          2 Answers
          2






          active

          oldest

          votes


















          2














          Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.



          In your pipeline, the contents of the /var/log/messages file is piped into the input of the date command by running cat on the left hand side of the pipeline.



          The date command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.



          The net visible result is that the output of the cat is ignored and discarded, while the output of date is show in the terminal (or wherever the output of the pipeline goes).



          What actually happens with the output of the cat command is that, since date won't read it, the cat command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date will never read it). The date command does its thing and outputs its string, after which it terminates.



          At that point, when date terminates and the standard input stream of date is closed, the cat command receives a PIPE signal by the shell. This signal tells cat that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE is the default action for this signal). The rest of the file that cat was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.



          The exact same thing happens with date replaced by w, or any other command that does not read its standard input stream.



          You can compare this with a using a command that actually does read its standard input stream:



          cat /var/log/messages | tr '[:lower:]' '[:upper:]'


          or, without the pipe (as the cat is actually not needed in any of these examples),



          tr '[:lower:]' '[:upper:]' </var/log/messages





          share|improve this answer
































            0














            That's how pipes work: The left hand side command's output is send as input into the right hand side command.



            If you want to run two commands one after another, use a semicolon:



            cat /var/log/messages ; date


            If you want to run the second command only if the first one is successful, use &&:



            cat /var/log/messages && date


            If you want to run the second command only if the first one fails, use ||:



            cat /var/log/messages || date


            If you want to run the first command in the background and run the second one at the same time, use &



            cat /var/log/messages & date





            share|improve this answer























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511215%2fonly-command-after-pipe-executes-how-does-the-pipe-work-in-these-scenarios%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.



              In your pipeline, the contents of the /var/log/messages file is piped into the input of the date command by running cat on the left hand side of the pipeline.



              The date command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.



              The net visible result is that the output of the cat is ignored and discarded, while the output of date is show in the terminal (or wherever the output of the pipeline goes).



              What actually happens with the output of the cat command is that, since date won't read it, the cat command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date will never read it). The date command does its thing and outputs its string, after which it terminates.



              At that point, when date terminates and the standard input stream of date is closed, the cat command receives a PIPE signal by the shell. This signal tells cat that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE is the default action for this signal). The rest of the file that cat was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.



              The exact same thing happens with date replaced by w, or any other command that does not read its standard input stream.



              You can compare this with a using a command that actually does read its standard input stream:



              cat /var/log/messages | tr '[:lower:]' '[:upper:]'


              or, without the pipe (as the cat is actually not needed in any of these examples),



              tr '[:lower:]' '[:upper:]' </var/log/messages





              share|improve this answer





























                2














                Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.



                In your pipeline, the contents of the /var/log/messages file is piped into the input of the date command by running cat on the left hand side of the pipeline.



                The date command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.



                The net visible result is that the output of the cat is ignored and discarded, while the output of date is show in the terminal (or wherever the output of the pipeline goes).



                What actually happens with the output of the cat command is that, since date won't read it, the cat command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date will never read it). The date command does its thing and outputs its string, after which it terminates.



                At that point, when date terminates and the standard input stream of date is closed, the cat command receives a PIPE signal by the shell. This signal tells cat that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE is the default action for this signal). The rest of the file that cat was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.



                The exact same thing happens with date replaced by w, or any other command that does not read its standard input stream.



                You can compare this with a using a command that actually does read its standard input stream:



                cat /var/log/messages | tr '[:lower:]' '[:upper:]'


                or, without the pipe (as the cat is actually not needed in any of these examples),



                tr '[:lower:]' '[:upper:]' </var/log/messages





                share|improve this answer



























                  2












                  2








                  2







                  Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.



                  In your pipeline, the contents of the /var/log/messages file is piped into the input of the date command by running cat on the left hand side of the pipeline.



                  The date command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.



                  The net visible result is that the output of the cat is ignored and discarded, while the output of date is show in the terminal (or wherever the output of the pipeline goes).



                  What actually happens with the output of the cat command is that, since date won't read it, the cat command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date will never read it). The date command does its thing and outputs its string, after which it terminates.



                  At that point, when date terminates and the standard input stream of date is closed, the cat command receives a PIPE signal by the shell. This signal tells cat that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE is the default action for this signal). The rest of the file that cat was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.



                  The exact same thing happens with date replaced by w, or any other command that does not read its standard input stream.



                  You can compare this with a using a command that actually does read its standard input stream:



                  cat /var/log/messages | tr '[:lower:]' '[:upper:]'


                  or, without the pipe (as the cat is actually not needed in any of these examples),



                  tr '[:lower:]' '[:upper:]' </var/log/messages





                  share|improve this answer















                  Both commands in both examples are executed. Only the output of the command after the pipe symbol is actually shown.



                  In your pipeline, the contents of the /var/log/messages file is piped into the input of the date command by running cat on the left hand side of the pipeline.



                  The date command does not care about its standard input stream and will ignore any data arriving there. It will, however, produce its own output, which it does on its standard output stream.



                  The net visible result is that the output of the cat is ignored and discarded, while the output of date is show in the terminal (or wherever the output of the pipeline goes).



                  What actually happens with the output of the cat command is that, since date won't read it, the cat command is temporarily blocked, waiting for its output to be read (after successfully outputting however much the pipe's buffer can hold; it does not know date will never read it). The date command does its thing and outputs its string, after which it terminates.



                  At that point, when date terminates and the standard input stream of date is closed, the cat command receives a PIPE signal by the shell. This signal tells cat that whatever data it's trying to write to its standard output is never going to be read, and it too terminates (terminating on PIPE is the default action for this signal). The rest of the file that cat was reading from is never read, and the data in the pipe's buffer is discarded when the shell releases the memory associated with it.



                  The exact same thing happens with date replaced by w, or any other command that does not read its standard input stream.



                  You can compare this with a using a command that actually does read its standard input stream:



                  cat /var/log/messages | tr '[:lower:]' '[:upper:]'


                  or, without the pipe (as the cat is actually not needed in any of these examples),



                  tr '[:lower:]' '[:upper:]' </var/log/messages






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 8 at 12:24

























                  answered Apr 8 at 12:05









                  KusalanandaKusalananda

                  141k17263439




                  141k17263439























                      0














                      That's how pipes work: The left hand side command's output is send as input into the right hand side command.



                      If you want to run two commands one after another, use a semicolon:



                      cat /var/log/messages ; date


                      If you want to run the second command only if the first one is successful, use &&:



                      cat /var/log/messages && date


                      If you want to run the second command only if the first one fails, use ||:



                      cat /var/log/messages || date


                      If you want to run the first command in the background and run the second one at the same time, use &



                      cat /var/log/messages & date





                      share|improve this answer



























                        0














                        That's how pipes work: The left hand side command's output is send as input into the right hand side command.



                        If you want to run two commands one after another, use a semicolon:



                        cat /var/log/messages ; date


                        If you want to run the second command only if the first one is successful, use &&:



                        cat /var/log/messages && date


                        If you want to run the second command only if the first one fails, use ||:



                        cat /var/log/messages || date


                        If you want to run the first command in the background and run the second one at the same time, use &



                        cat /var/log/messages & date





                        share|improve this answer

























                          0












                          0








                          0







                          That's how pipes work: The left hand side command's output is send as input into the right hand side command.



                          If you want to run two commands one after another, use a semicolon:



                          cat /var/log/messages ; date


                          If you want to run the second command only if the first one is successful, use &&:



                          cat /var/log/messages && date


                          If you want to run the second command only if the first one fails, use ||:



                          cat /var/log/messages || date


                          If you want to run the first command in the background and run the second one at the same time, use &



                          cat /var/log/messages & date





                          share|improve this answer













                          That's how pipes work: The left hand side command's output is send as input into the right hand side command.



                          If you want to run two commands one after another, use a semicolon:



                          cat /var/log/messages ; date


                          If you want to run the second command only if the first one is successful, use &&:



                          cat /var/log/messages && date


                          If you want to run the second command only if the first one fails, use ||:



                          cat /var/log/messages || date


                          If you want to run the first command in the background and run the second one at the same time, use &



                          cat /var/log/messages & date






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 8 at 11:52









                          chorobachoroba

                          27.1k45176




                          27.1k45176



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511215%2fonly-command-after-pipe-executes-how-does-the-pipe-work-in-these-scenarios%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                              Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                              Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.