Cursor disappears when running `top -n1 | head`

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











up vote
3
down vote

favorite












When running



top -n1 | head


the terminal's cursor disappears.
Running top -n1 brings it back.



Tested in gnome-terminal and tilix in Ubuntu 16.04 and CentOS 7.5.




Running top -n1 | tail doesn't have this issue, so I think, something at the end of top output let the cursor reappear which is not executed when printing the head only.



What causes this and how can I get back the cursor more elegantly ?










share|improve this question























  • I can also get it back with running tput cnorm. (via)
    – RoVo
    51 mins ago















up vote
3
down vote

favorite












When running



top -n1 | head


the terminal's cursor disappears.
Running top -n1 brings it back.



Tested in gnome-terminal and tilix in Ubuntu 16.04 and CentOS 7.5.




Running top -n1 | tail doesn't have this issue, so I think, something at the end of top output let the cursor reappear which is not executed when printing the head only.



What causes this and how can I get back the cursor more elegantly ?










share|improve this question























  • I can also get it back with running tput cnorm. (via)
    – RoVo
    51 mins ago













up vote
3
down vote

favorite









up vote
3
down vote

favorite











When running



top -n1 | head


the terminal's cursor disappears.
Running top -n1 brings it back.



Tested in gnome-terminal and tilix in Ubuntu 16.04 and CentOS 7.5.




Running top -n1 | tail doesn't have this issue, so I think, something at the end of top output let the cursor reappear which is not executed when printing the head only.



What causes this and how can I get back the cursor more elegantly ?










share|improve this question















When running



top -n1 | head


the terminal's cursor disappears.
Running top -n1 brings it back.



Tested in gnome-terminal and tilix in Ubuntu 16.04 and CentOS 7.5.




Running top -n1 | tail doesn't have this issue, so I think, something at the end of top output let the cursor reappear which is not executed when printing the head only.



What causes this and how can I get back the cursor more elegantly ?







terminal gnome-terminal top cursor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 1 hour ago









RoVo

1,416211




1,416211











  • I can also get it back with running tput cnorm. (via)
    – RoVo
    51 mins ago

















  • I can also get it back with running tput cnorm. (via)
    – RoVo
    51 mins ago
















I can also get it back with running tput cnorm. (via)
– RoVo
51 mins ago





I can also get it back with running tput cnorm. (via)
– RoVo
51 mins ago











1 Answer
1






active

oldest

votes

















up vote
5
down vote













Best way IMHO is to make top use "batch" mode which is intended to be used with non-interactive use cases such as piping to another program or to a file.



So, this



top -n1 -b | head


won't be leaving the shell without a cursor.



As for the why the cursor disappears...



Since top is an interactive program, it "messes" with the terminal in order to grab input, scroll content, etc, and it hides the cursor.



When terminating it has to restore the cursor display, and it does so by sending a control code to the terminal itself.



By piping the command through head, this control code won't get through (head prints just the firs 10 lines by default, and the output of top + control codes to restore the terminal state is >10 lines).



As a matter of fact, if you give head enough lines to print, the cursor appears!



For example,



top -n1 | head -n100


leaves a cursor on my system.






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%2f469770%2fcursor-disappears-when-running-top-n1-head%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    Best way IMHO is to make top use "batch" mode which is intended to be used with non-interactive use cases such as piping to another program or to a file.



    So, this



    top -n1 -b | head


    won't be leaving the shell without a cursor.



    As for the why the cursor disappears...



    Since top is an interactive program, it "messes" with the terminal in order to grab input, scroll content, etc, and it hides the cursor.



    When terminating it has to restore the cursor display, and it does so by sending a control code to the terminal itself.



    By piping the command through head, this control code won't get through (head prints just the firs 10 lines by default, and the output of top + control codes to restore the terminal state is >10 lines).



    As a matter of fact, if you give head enough lines to print, the cursor appears!



    For example,



    top -n1 | head -n100


    leaves a cursor on my system.






    share|improve this answer


























      up vote
      5
      down vote













      Best way IMHO is to make top use "batch" mode which is intended to be used with non-interactive use cases such as piping to another program or to a file.



      So, this



      top -n1 -b | head


      won't be leaving the shell without a cursor.



      As for the why the cursor disappears...



      Since top is an interactive program, it "messes" with the terminal in order to grab input, scroll content, etc, and it hides the cursor.



      When terminating it has to restore the cursor display, and it does so by sending a control code to the terminal itself.



      By piping the command through head, this control code won't get through (head prints just the firs 10 lines by default, and the output of top + control codes to restore the terminal state is >10 lines).



      As a matter of fact, if you give head enough lines to print, the cursor appears!



      For example,



      top -n1 | head -n100


      leaves a cursor on my system.






      share|improve this answer
























        up vote
        5
        down vote










        up vote
        5
        down vote









        Best way IMHO is to make top use "batch" mode which is intended to be used with non-interactive use cases such as piping to another program or to a file.



        So, this



        top -n1 -b | head


        won't be leaving the shell without a cursor.



        As for the why the cursor disappears...



        Since top is an interactive program, it "messes" with the terminal in order to grab input, scroll content, etc, and it hides the cursor.



        When terminating it has to restore the cursor display, and it does so by sending a control code to the terminal itself.



        By piping the command through head, this control code won't get through (head prints just the firs 10 lines by default, and the output of top + control codes to restore the terminal state is >10 lines).



        As a matter of fact, if you give head enough lines to print, the cursor appears!



        For example,



        top -n1 | head -n100


        leaves a cursor on my system.






        share|improve this answer














        Best way IMHO is to make top use "batch" mode which is intended to be used with non-interactive use cases such as piping to another program or to a file.



        So, this



        top -n1 -b | head


        won't be leaving the shell without a cursor.



        As for the why the cursor disappears...



        Since top is an interactive program, it "messes" with the terminal in order to grab input, scroll content, etc, and it hides the cursor.



        When terminating it has to restore the cursor display, and it does so by sending a control code to the terminal itself.



        By piping the command through head, this control code won't get through (head prints just the firs 10 lines by default, and the output of top + control codes to restore the terminal state is >10 lines).



        As a matter of fact, if you give head enough lines to print, the cursor appears!



        For example,



        top -n1 | head -n100


        leaves a cursor on my system.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 1 hour ago









        Mr Shunz

        2,23711617




        2,23711617



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f469770%2fcursor-disappears-when-running-top-n1-head%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            One-line joke