Strange output when using tee in pipe command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I had to use the following command in an assignment:



who|tee test|wc -l


The output on my system is below, which implies that 2 users are connected:



2


Why is it I don't get the output of who on the screen, and right after that the output of wc -l? I thought tee wrote the output to the screen and created a file with the same output at the same time?



The output of who is however found in my file "test", but still it doesn't make sense to me.







share|improve this question


























    up vote
    1
    down vote

    favorite












    I had to use the following command in an assignment:



    who|tee test|wc -l


    The output on my system is below, which implies that 2 users are connected:



    2


    Why is it I don't get the output of who on the screen, and right after that the output of wc -l? I thought tee wrote the output to the screen and created a file with the same output at the same time?



    The output of who is however found in my file "test", but still it doesn't make sense to me.







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I had to use the following command in an assignment:



      who|tee test|wc -l


      The output on my system is below, which implies that 2 users are connected:



      2


      Why is it I don't get the output of who on the screen, and right after that the output of wc -l? I thought tee wrote the output to the screen and created a file with the same output at the same time?



      The output of who is however found in my file "test", but still it doesn't make sense to me.







      share|improve this question














      I had to use the following command in an assignment:



      who|tee test|wc -l


      The output on my system is below, which implies that 2 users are connected:



      2


      Why is it I don't get the output of who on the screen, and right after that the output of wc -l? I thought tee wrote the output to the screen and created a file with the same output at the same time?



      The output of who is however found in my file "test", but still it doesn't make sense to me.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 22 '17 at 22:40









      agc

      4,1501935




      4,1501935










      asked Oct 22 '17 at 22:06









      Omnicron

      82




      82




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          The effect of piping to tee is that whatever your first command writes to its standard output is written to a file (whose name you passed as a command-line argument to tee) as well as written to the standard output of the tee command. If the pipeline doesn't continue and you don't perform any redirections on the tee command, then tee's standard output is that of your shell, usually your terminal.



          That's why running who and running who | tee test show the same text on your terminal. The difference with tee you also write it to a file.



          If the pipeline continues, as with who | tee test | wc -l, then whatever standard output would have been written to the terminal is sent to the next command in the pipeline instead. This is the wc command and, unlike tee, wc does not copy its input to standard output (or anywhere). Instead it shows statistics. With the -l option it shows just line counts, so that's all you see.



          So the reason you see just 2 from who | tee test | wc -l is the same as the reason you see just 2 from who | wc -l. The tee command writes the output of who to a file but it does not cause it to be printed to the terminal unless its standard output is the terminal. By default it usually is, but not when you pipe it to yet another command.



          If you've seen a command on the left side of a | whose output is displayed on the terminal rather than being used as input to the next command in the pipeline, then it likely was writing to standard error instead of standard output.






          share|improve this answer



























            up vote
            1
            down vote













            tee duplicates input so it goes to the file and stdout. From there stdout is piped to wc...it becomes stdin for wc. wc just returns counts, the input data it counts isn't echoed.



            If you're trying to make sense of it you could think of cmd1 | tee file | cmd2 as having the same effect as this:



            cmd1 > file
            cat file | cmd2


            This isn't what actually happens but from the standpoint of the person executing the command they would appear to be the same (as long as cmd1 is fast enough to give the impression that everything happens almost simultaneously). In reality file is being filled with data at the same time that cmd2 is receiving that data.






            share|improve this answer





























              up vote
              0
              down vote













              To see both who and wc outputs in bash do:



              who | tee >(wc -l) test


              Output:



              me tty7 2017-10-16 18:17 (:0)
              1


              Along with the contents of test.






              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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f399787%2fstrange-output-when-using-tee-in-pipe-command%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                The effect of piping to tee is that whatever your first command writes to its standard output is written to a file (whose name you passed as a command-line argument to tee) as well as written to the standard output of the tee command. If the pipeline doesn't continue and you don't perform any redirections on the tee command, then tee's standard output is that of your shell, usually your terminal.



                That's why running who and running who | tee test show the same text on your terminal. The difference with tee you also write it to a file.



                If the pipeline continues, as with who | tee test | wc -l, then whatever standard output would have been written to the terminal is sent to the next command in the pipeline instead. This is the wc command and, unlike tee, wc does not copy its input to standard output (or anywhere). Instead it shows statistics. With the -l option it shows just line counts, so that's all you see.



                So the reason you see just 2 from who | tee test | wc -l is the same as the reason you see just 2 from who | wc -l. The tee command writes the output of who to a file but it does not cause it to be printed to the terminal unless its standard output is the terminal. By default it usually is, but not when you pipe it to yet another command.



                If you've seen a command on the left side of a | whose output is displayed on the terminal rather than being used as input to the next command in the pipeline, then it likely was writing to standard error instead of standard output.






                share|improve this answer
























                  up vote
                  2
                  down vote



                  accepted










                  The effect of piping to tee is that whatever your first command writes to its standard output is written to a file (whose name you passed as a command-line argument to tee) as well as written to the standard output of the tee command. If the pipeline doesn't continue and you don't perform any redirections on the tee command, then tee's standard output is that of your shell, usually your terminal.



                  That's why running who and running who | tee test show the same text on your terminal. The difference with tee you also write it to a file.



                  If the pipeline continues, as with who | tee test | wc -l, then whatever standard output would have been written to the terminal is sent to the next command in the pipeline instead. This is the wc command and, unlike tee, wc does not copy its input to standard output (or anywhere). Instead it shows statistics. With the -l option it shows just line counts, so that's all you see.



                  So the reason you see just 2 from who | tee test | wc -l is the same as the reason you see just 2 from who | wc -l. The tee command writes the output of who to a file but it does not cause it to be printed to the terminal unless its standard output is the terminal. By default it usually is, but not when you pipe it to yet another command.



                  If you've seen a command on the left side of a | whose output is displayed on the terminal rather than being used as input to the next command in the pipeline, then it likely was writing to standard error instead of standard output.






                  share|improve this answer






















                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    The effect of piping to tee is that whatever your first command writes to its standard output is written to a file (whose name you passed as a command-line argument to tee) as well as written to the standard output of the tee command. If the pipeline doesn't continue and you don't perform any redirections on the tee command, then tee's standard output is that of your shell, usually your terminal.



                    That's why running who and running who | tee test show the same text on your terminal. The difference with tee you also write it to a file.



                    If the pipeline continues, as with who | tee test | wc -l, then whatever standard output would have been written to the terminal is sent to the next command in the pipeline instead. This is the wc command and, unlike tee, wc does not copy its input to standard output (or anywhere). Instead it shows statistics. With the -l option it shows just line counts, so that's all you see.



                    So the reason you see just 2 from who | tee test | wc -l is the same as the reason you see just 2 from who | wc -l. The tee command writes the output of who to a file but it does not cause it to be printed to the terminal unless its standard output is the terminal. By default it usually is, but not when you pipe it to yet another command.



                    If you've seen a command on the left side of a | whose output is displayed on the terminal rather than being used as input to the next command in the pipeline, then it likely was writing to standard error instead of standard output.






                    share|improve this answer












                    The effect of piping to tee is that whatever your first command writes to its standard output is written to a file (whose name you passed as a command-line argument to tee) as well as written to the standard output of the tee command. If the pipeline doesn't continue and you don't perform any redirections on the tee command, then tee's standard output is that of your shell, usually your terminal.



                    That's why running who and running who | tee test show the same text on your terminal. The difference with tee you also write it to a file.



                    If the pipeline continues, as with who | tee test | wc -l, then whatever standard output would have been written to the terminal is sent to the next command in the pipeline instead. This is the wc command and, unlike tee, wc does not copy its input to standard output (or anywhere). Instead it shows statistics. With the -l option it shows just line counts, so that's all you see.



                    So the reason you see just 2 from who | tee test | wc -l is the same as the reason you see just 2 from who | wc -l. The tee command writes the output of who to a file but it does not cause it to be printed to the terminal unless its standard output is the terminal. By default it usually is, but not when you pipe it to yet another command.



                    If you've seen a command on the left side of a | whose output is displayed on the terminal rather than being used as input to the next command in the pipeline, then it likely was writing to standard error instead of standard output.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 22 '17 at 22:41









                    Eliah Kagan

                    3,16221530




                    3,16221530






















                        up vote
                        1
                        down vote













                        tee duplicates input so it goes to the file and stdout. From there stdout is piped to wc...it becomes stdin for wc. wc just returns counts, the input data it counts isn't echoed.



                        If you're trying to make sense of it you could think of cmd1 | tee file | cmd2 as having the same effect as this:



                        cmd1 > file
                        cat file | cmd2


                        This isn't what actually happens but from the standpoint of the person executing the command they would appear to be the same (as long as cmd1 is fast enough to give the impression that everything happens almost simultaneously). In reality file is being filled with data at the same time that cmd2 is receiving that data.






                        share|improve this answer


























                          up vote
                          1
                          down vote













                          tee duplicates input so it goes to the file and stdout. From there stdout is piped to wc...it becomes stdin for wc. wc just returns counts, the input data it counts isn't echoed.



                          If you're trying to make sense of it you could think of cmd1 | tee file | cmd2 as having the same effect as this:



                          cmd1 > file
                          cat file | cmd2


                          This isn't what actually happens but from the standpoint of the person executing the command they would appear to be the same (as long as cmd1 is fast enough to give the impression that everything happens almost simultaneously). In reality file is being filled with data at the same time that cmd2 is receiving that data.






                          share|improve this answer
























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            tee duplicates input so it goes to the file and stdout. From there stdout is piped to wc...it becomes stdin for wc. wc just returns counts, the input data it counts isn't echoed.



                            If you're trying to make sense of it you could think of cmd1 | tee file | cmd2 as having the same effect as this:



                            cmd1 > file
                            cat file | cmd2


                            This isn't what actually happens but from the standpoint of the person executing the command they would appear to be the same (as long as cmd1 is fast enough to give the impression that everything happens almost simultaneously). In reality file is being filled with data at the same time that cmd2 is receiving that data.






                            share|improve this answer














                            tee duplicates input so it goes to the file and stdout. From there stdout is piped to wc...it becomes stdin for wc. wc just returns counts, the input data it counts isn't echoed.



                            If you're trying to make sense of it you could think of cmd1 | tee file | cmd2 as having the same effect as this:



                            cmd1 > file
                            cat file | cmd2


                            This isn't what actually happens but from the standpoint of the person executing the command they would appear to be the same (as long as cmd1 is fast enough to give the impression that everything happens almost simultaneously). In reality file is being filled with data at the same time that cmd2 is receiving that data.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Oct 22 '17 at 22:40

























                            answered Oct 22 '17 at 22:34









                            B Layer

                            3,9241525




                            3,9241525




















                                up vote
                                0
                                down vote













                                To see both who and wc outputs in bash do:



                                who | tee >(wc -l) test


                                Output:



                                me tty7 2017-10-16 18:17 (:0)
                                1


                                Along with the contents of test.






                                share|improve this answer


























                                  up vote
                                  0
                                  down vote













                                  To see both who and wc outputs in bash do:



                                  who | tee >(wc -l) test


                                  Output:



                                  me tty7 2017-10-16 18:17 (:0)
                                  1


                                  Along with the contents of test.






                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    To see both who and wc outputs in bash do:



                                    who | tee >(wc -l) test


                                    Output:



                                    me tty7 2017-10-16 18:17 (:0)
                                    1


                                    Along with the contents of test.






                                    share|improve this answer














                                    To see both who and wc outputs in bash do:



                                    who | tee >(wc -l) test


                                    Output:



                                    me tty7 2017-10-16 18:17 (:0)
                                    1


                                    Along with the contents of test.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Oct 22 '17 at 22:36

























                                    answered Oct 22 '17 at 22:28









                                    agc

                                    4,1501935




                                    4,1501935



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399787%2fstrange-output-when-using-tee-in-pipe-command%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        Peggy Mitchell

                                        Palaiologos

                                        The Forum (Inglewood, California)