How does clear command work?

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





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








32















I was recently trying to learn more about how the shell works and was looking at how the clear command works. The executable is located in /usr/bin/clear and it seems to print out a bunch of blank lines (equal to the height of the terminal) and puts the cursor at the top-left of the terminal.



The output of the command is always the same, regardless of the size of the terminal:



$ clear | hexdump -C
00000000 1b 5b 48 1b 5b 32 4a |.[H.[2J|
00000007


and can be replicated with the echo having the exact same effect:



$ /bin/echo -e "x1bx5bx48x1bx5bx32x4ac"


I was really curious how this output of this command translates to clearing the console.










share|improve this question






















  • CTRL+L - look at stty -a

    – mikeserv
    Apr 14 '14 at 19:22


















32















I was recently trying to learn more about how the shell works and was looking at how the clear command works. The executable is located in /usr/bin/clear and it seems to print out a bunch of blank lines (equal to the height of the terminal) and puts the cursor at the top-left of the terminal.



The output of the command is always the same, regardless of the size of the terminal:



$ clear | hexdump -C
00000000 1b 5b 48 1b 5b 32 4a |.[H.[2J|
00000007


and can be replicated with the echo having the exact same effect:



$ /bin/echo -e "x1bx5bx48x1bx5bx32x4ac"


I was really curious how this output of this command translates to clearing the console.










share|improve this question






















  • CTRL+L - look at stty -a

    – mikeserv
    Apr 14 '14 at 19:22














32












32








32


9






I was recently trying to learn more about how the shell works and was looking at how the clear command works. The executable is located in /usr/bin/clear and it seems to print out a bunch of blank lines (equal to the height of the terminal) and puts the cursor at the top-left of the terminal.



The output of the command is always the same, regardless of the size of the terminal:



$ clear | hexdump -C
00000000 1b 5b 48 1b 5b 32 4a |.[H.[2J|
00000007


and can be replicated with the echo having the exact same effect:



$ /bin/echo -e "x1bx5bx48x1bx5bx32x4ac"


I was really curious how this output of this command translates to clearing the console.










share|improve this question














I was recently trying to learn more about how the shell works and was looking at how the clear command works. The executable is located in /usr/bin/clear and it seems to print out a bunch of blank lines (equal to the height of the terminal) and puts the cursor at the top-left of the terminal.



The output of the command is always the same, regardless of the size of the terminal:



$ clear | hexdump -C
00000000 1b 5b 48 1b 5b 32 4a |.[H.[2J|
00000007


and can be replicated with the echo having the exact same effect:



$ /bin/echo -e "x1bx5bx48x1bx5bx32x4ac"


I was really curious how this output of this command translates to clearing the console.







shell terminal






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 14 '14 at 19:17









EricEric

266135




266135












  • CTRL+L - look at stty -a

    – mikeserv
    Apr 14 '14 at 19:22


















  • CTRL+L - look at stty -a

    – mikeserv
    Apr 14 '14 at 19:22

















CTRL+L - look at stty -a

– mikeserv
Apr 14 '14 at 19:22






CTRL+L - look at stty -a

– mikeserv
Apr 14 '14 at 19:22











6 Answers
6






active

oldest

votes


















26














The output of the clear command is console escape codes. The exact codes required depend on the exact terminal you are using, however most use ANSI control sequences. Here is a good link explaining the various codes - http://www.termsys.demon.co.uk/vtansi.htm. The relevant snippets are:



Cursor Home <ESC>[ROW;COLUMNH

Sets the cursor position where subsequent text will begin. If no row/column
parameters are provided (ie. <ESC>[H), the cursor will move to the home position,
at the upper left of the screen.


And:



Erase Screen <ESC>[2J

Erases the screen with the background colour and moves the cursor to home.


Where <ESC> is hex 1B or octal 033. Another way to view the characters is with:



clear | sed -n l





share|improve this answer
































    18














    It works by issuing certain ANSI escape sequences. Specifically, these two:




    Esc[Line;ColumnH          Cursor Position:

    Esc[Line;Columnf            Moves the cursor to the specified position (coordinates).
    If you do not

                                             specify a position, the cursor moves to the home position at the upper-left

                                             corner of the screen (line 0, column 0).



    Esc[2J                              Erase Display:

                                             Clears the screen and moves the cursor to the home position
                                             (line 0, column 0).




    This is perhaps easier to understand in the output of od -c:



    $ clear | od -c
    0000000 033 [ H 033 [ 2 J
    0000007


    033 is Esc, so the output above is simply Esc[H and then Esc[2J.






    share|improve this answer






























      9














      The output sent by clear(1) depends on your terminal type, defined by $TERM in the shell environment. It does the same thing as the command "tput clear", which is looking up the escape code for the current terminal type and sending that string to standard output.



      The terminal receiving the escape code from clear/tput interprets it and executes the command sent, such as clearing the local display. "terminal" means the local console or a terminal session (putty, xterm, etc.), possibly via ssh or telnet.






      share|improve this answer






























        7














        I'm surprised other answers have not mentioned TERMINFO (or TERMCAP)



        Use the man pages Luke



        man clear says ...



        NAME
        clear - clear the terminal screen

        SYNOPSIS
        clear

        DESCRIPTION
        clear clears your screen if this is possible. It looks in the environ-
        ment for the terminal type and then in the terminfo database to figure
        out how to clear the screen.


        TERM



        The clear command only uses ANSI escape sequences if your $TERM is set to ANSI or some ANSI-based terminal type such as XTERM.



        $ TERM=ansi clear | hexdump -C
        00000000 1b 5b 48 1b 5b 4a |.[H.[J|
        00000006

        $ TERM=wy50 clear | hexdump -C
        00000000 1b 2b |.+|
        00000002

        $ TERM=hurd clear | hexdump -C
        00000000 1b 63 |.c|
        00000002

        $ TERM=glasstty clear | hexdump -C
        00000000 0c |.|
        00000001

        $ TERM=vt52 clear | hexdump -C
        00000000 1b 48 1b 4a |.H.J|
        00000004

        $ TERM=hpterm clear | hexdump -C
        00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
        00000009


        INFOCMP



        You can use infocmp to investigate



        $ infocmp -L -1 hpterm | grep clear_screen
        clear_screen=E&a0y0CEJ,


        TPUT



        Or you can use tput to view a capability



        $ tput -T hpterm clear | hexdump -C
        00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
        00000009


        $ tput -T hpterm reset | hexdump -C
        00000000 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
        00000010 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 | .1 .1 |
        00000020 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 | .1 |
        00000030 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 |.1 .1 |
        00000040 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 | .1 .1|
        00000050 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
        00000060 20 20 1b 31 | .1|
        00000064





        share|improve this answer
































          6














          In addition to all nice answer above, we can do some strace to see what happen:



          $ strace -e trace=write echo -e "x1bx5bx48x1bx5bx32x4ac"
          write(1, "33[H33[2J", 7

          $ strace -e trace=write clear
          write(1, "33[H33[2J", 7


          You can see, two command provide the same ANSI escape sequences.






          share|improve this answer






























            0














            Firstly /bin/echo -e "x1bx5bx48x1bx5bx32x4ac" doesn't really clear the screen. You can scroll up to see the previous contents.



            Secondly, I opened IRB (which is an interactive Ruby shell), and typed:



            p `clear`


            Or



            p %x(clear)



            Or:



            require 'open3'
            p Open3.capture2('clear')[0]


            All the codes should return "e[3Je[He[2J"



            Now open your terminal, type echo -e "e[3Je[He[2J"



            It should clear the screen. These are called ANSI escape sequences:



            https://en.wikipedia.org/wiki/ANSI_escape_code



            You can use these codes to blink a text (e[5m), colourize a text: (for i in 0..255 ; do printf "e[38;5;$im$i " ; done ; echo) and many other things!






            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%2f124762%2fhow-does-clear-command-work%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              26














              The output of the clear command is console escape codes. The exact codes required depend on the exact terminal you are using, however most use ANSI control sequences. Here is a good link explaining the various codes - http://www.termsys.demon.co.uk/vtansi.htm. The relevant snippets are:



              Cursor Home <ESC>[ROW;COLUMNH

              Sets the cursor position where subsequent text will begin. If no row/column
              parameters are provided (ie. <ESC>[H), the cursor will move to the home position,
              at the upper left of the screen.


              And:



              Erase Screen <ESC>[2J

              Erases the screen with the background colour and moves the cursor to home.


              Where <ESC> is hex 1B or octal 033. Another way to view the characters is with:



              clear | sed -n l





              share|improve this answer





























                26














                The output of the clear command is console escape codes. The exact codes required depend on the exact terminal you are using, however most use ANSI control sequences. Here is a good link explaining the various codes - http://www.termsys.demon.co.uk/vtansi.htm. The relevant snippets are:



                Cursor Home <ESC>[ROW;COLUMNH

                Sets the cursor position where subsequent text will begin. If no row/column
                parameters are provided (ie. <ESC>[H), the cursor will move to the home position,
                at the upper left of the screen.


                And:



                Erase Screen <ESC>[2J

                Erases the screen with the background colour and moves the cursor to home.


                Where <ESC> is hex 1B or octal 033. Another way to view the characters is with:



                clear | sed -n l





                share|improve this answer



























                  26












                  26








                  26







                  The output of the clear command is console escape codes. The exact codes required depend on the exact terminal you are using, however most use ANSI control sequences. Here is a good link explaining the various codes - http://www.termsys.demon.co.uk/vtansi.htm. The relevant snippets are:



                  Cursor Home <ESC>[ROW;COLUMNH

                  Sets the cursor position where subsequent text will begin. If no row/column
                  parameters are provided (ie. <ESC>[H), the cursor will move to the home position,
                  at the upper left of the screen.


                  And:



                  Erase Screen <ESC>[2J

                  Erases the screen with the background colour and moves the cursor to home.


                  Where <ESC> is hex 1B or octal 033. Another way to view the characters is with:



                  clear | sed -n l





                  share|improve this answer















                  The output of the clear command is console escape codes. The exact codes required depend on the exact terminal you are using, however most use ANSI control sequences. Here is a good link explaining the various codes - http://www.termsys.demon.co.uk/vtansi.htm. The relevant snippets are:



                  Cursor Home <ESC>[ROW;COLUMNH

                  Sets the cursor position where subsequent text will begin. If no row/column
                  parameters are provided (ie. <ESC>[H), the cursor will move to the home position,
                  at the upper left of the screen.


                  And:



                  Erase Screen <ESC>[2J

                  Erases the screen with the background colour and moves the cursor to home.


                  Where <ESC> is hex 1B or octal 033. Another way to view the characters is with:



                  clear | sed -n l






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 14 '14 at 19:50

























                  answered Apr 14 '14 at 19:44









                  GraemeGraeme

                  25.5k46699




                  25.5k46699























                      18














                      It works by issuing certain ANSI escape sequences. Specifically, these two:




                      Esc[Line;ColumnH          Cursor Position:

                      Esc[Line;Columnf            Moves the cursor to the specified position (coordinates).
                      If you do not

                                                               specify a position, the cursor moves to the home position at the upper-left

                                                               corner of the screen (line 0, column 0).



                      Esc[2J                              Erase Display:

                                                               Clears the screen and moves the cursor to the home position
                                                               (line 0, column 0).




                      This is perhaps easier to understand in the output of od -c:



                      $ clear | od -c
                      0000000 033 [ H 033 [ 2 J
                      0000007


                      033 is Esc, so the output above is simply Esc[H and then Esc[2J.






                      share|improve this answer



























                        18














                        It works by issuing certain ANSI escape sequences. Specifically, these two:




                        Esc[Line;ColumnH          Cursor Position:

                        Esc[Line;Columnf            Moves the cursor to the specified position (coordinates).
                        If you do not

                                                                 specify a position, the cursor moves to the home position at the upper-left

                                                                 corner of the screen (line 0, column 0).



                        Esc[2J                              Erase Display:

                                                                 Clears the screen and moves the cursor to the home position
                                                                 (line 0, column 0).




                        This is perhaps easier to understand in the output of od -c:



                        $ clear | od -c
                        0000000 033 [ H 033 [ 2 J
                        0000007


                        033 is Esc, so the output above is simply Esc[H and then Esc[2J.






                        share|improve this answer

























                          18












                          18








                          18







                          It works by issuing certain ANSI escape sequences. Specifically, these two:




                          Esc[Line;ColumnH          Cursor Position:

                          Esc[Line;Columnf            Moves the cursor to the specified position (coordinates).
                          If you do not

                                                                   specify a position, the cursor moves to the home position at the upper-left

                                                                   corner of the screen (line 0, column 0).



                          Esc[2J                              Erase Display:

                                                                   Clears the screen and moves the cursor to the home position
                                                                   (line 0, column 0).




                          This is perhaps easier to understand in the output of od -c:



                          $ clear | od -c
                          0000000 033 [ H 033 [ 2 J
                          0000007


                          033 is Esc, so the output above is simply Esc[H and then Esc[2J.






                          share|improve this answer













                          It works by issuing certain ANSI escape sequences. Specifically, these two:




                          Esc[Line;ColumnH          Cursor Position:

                          Esc[Line;Columnf            Moves the cursor to the specified position (coordinates).
                          If you do not

                                                                   specify a position, the cursor moves to the home position at the upper-left

                                                                   corner of the screen (line 0, column 0).



                          Esc[2J                              Erase Display:

                                                                   Clears the screen and moves the cursor to the home position
                                                                   (line 0, column 0).




                          This is perhaps easier to understand in the output of od -c:



                          $ clear | od -c
                          0000000 033 [ H 033 [ 2 J
                          0000007


                          033 is Esc, so the output above is simply Esc[H and then Esc[2J.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 14 '14 at 19:44









                          terdonterdon

                          133k33268448




                          133k33268448





















                              9














                              The output sent by clear(1) depends on your terminal type, defined by $TERM in the shell environment. It does the same thing as the command "tput clear", which is looking up the escape code for the current terminal type and sending that string to standard output.



                              The terminal receiving the escape code from clear/tput interprets it and executes the command sent, such as clearing the local display. "terminal" means the local console or a terminal session (putty, xterm, etc.), possibly via ssh or telnet.






                              share|improve this answer



























                                9














                                The output sent by clear(1) depends on your terminal type, defined by $TERM in the shell environment. It does the same thing as the command "tput clear", which is looking up the escape code for the current terminal type and sending that string to standard output.



                                The terminal receiving the escape code from clear/tput interprets it and executes the command sent, such as clearing the local display. "terminal" means the local console or a terminal session (putty, xterm, etc.), possibly via ssh or telnet.






                                share|improve this answer

























                                  9












                                  9








                                  9







                                  The output sent by clear(1) depends on your terminal type, defined by $TERM in the shell environment. It does the same thing as the command "tput clear", which is looking up the escape code for the current terminal type and sending that string to standard output.



                                  The terminal receiving the escape code from clear/tput interprets it and executes the command sent, such as clearing the local display. "terminal" means the local console or a terminal session (putty, xterm, etc.), possibly via ssh or telnet.






                                  share|improve this answer













                                  The output sent by clear(1) depends on your terminal type, defined by $TERM in the shell environment. It does the same thing as the command "tput clear", which is looking up the escape code for the current terminal type and sending that string to standard output.



                                  The terminal receiving the escape code from clear/tput interprets it and executes the command sent, such as clearing the local display. "terminal" means the local console or a terminal session (putty, xterm, etc.), possibly via ssh or telnet.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Apr 14 '14 at 19:45









                                  mlgothmlgoth

                                  893




                                  893





















                                      7














                                      I'm surprised other answers have not mentioned TERMINFO (or TERMCAP)



                                      Use the man pages Luke



                                      man clear says ...



                                      NAME
                                      clear - clear the terminal screen

                                      SYNOPSIS
                                      clear

                                      DESCRIPTION
                                      clear clears your screen if this is possible. It looks in the environ-
                                      ment for the terminal type and then in the terminfo database to figure
                                      out how to clear the screen.


                                      TERM



                                      The clear command only uses ANSI escape sequences if your $TERM is set to ANSI or some ANSI-based terminal type such as XTERM.



                                      $ TERM=ansi clear | hexdump -C
                                      00000000 1b 5b 48 1b 5b 4a |.[H.[J|
                                      00000006

                                      $ TERM=wy50 clear | hexdump -C
                                      00000000 1b 2b |.+|
                                      00000002

                                      $ TERM=hurd clear | hexdump -C
                                      00000000 1b 63 |.c|
                                      00000002

                                      $ TERM=glasstty clear | hexdump -C
                                      00000000 0c |.|
                                      00000001

                                      $ TERM=vt52 clear | hexdump -C
                                      00000000 1b 48 1b 4a |.H.J|
                                      00000004

                                      $ TERM=hpterm clear | hexdump -C
                                      00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                      00000009


                                      INFOCMP



                                      You can use infocmp to investigate



                                      $ infocmp -L -1 hpterm | grep clear_screen
                                      clear_screen=E&a0y0CEJ,


                                      TPUT



                                      Or you can use tput to view a capability



                                      $ tput -T hpterm clear | hexdump -C
                                      00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                      00000009


                                      $ tput -T hpterm reset | hexdump -C
                                      00000000 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                      00000010 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 | .1 .1 |
                                      00000020 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 | .1 |
                                      00000030 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 |.1 .1 |
                                      00000040 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 | .1 .1|
                                      00000050 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                      00000060 20 20 1b 31 | .1|
                                      00000064





                                      share|improve this answer





























                                        7














                                        I'm surprised other answers have not mentioned TERMINFO (or TERMCAP)



                                        Use the man pages Luke



                                        man clear says ...



                                        NAME
                                        clear - clear the terminal screen

                                        SYNOPSIS
                                        clear

                                        DESCRIPTION
                                        clear clears your screen if this is possible. It looks in the environ-
                                        ment for the terminal type and then in the terminfo database to figure
                                        out how to clear the screen.


                                        TERM



                                        The clear command only uses ANSI escape sequences if your $TERM is set to ANSI or some ANSI-based terminal type such as XTERM.



                                        $ TERM=ansi clear | hexdump -C
                                        00000000 1b 5b 48 1b 5b 4a |.[H.[J|
                                        00000006

                                        $ TERM=wy50 clear | hexdump -C
                                        00000000 1b 2b |.+|
                                        00000002

                                        $ TERM=hurd clear | hexdump -C
                                        00000000 1b 63 |.c|
                                        00000002

                                        $ TERM=glasstty clear | hexdump -C
                                        00000000 0c |.|
                                        00000001

                                        $ TERM=vt52 clear | hexdump -C
                                        00000000 1b 48 1b 4a |.H.J|
                                        00000004

                                        $ TERM=hpterm clear | hexdump -C
                                        00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                        00000009


                                        INFOCMP



                                        You can use infocmp to investigate



                                        $ infocmp -L -1 hpterm | grep clear_screen
                                        clear_screen=E&a0y0CEJ,


                                        TPUT



                                        Or you can use tput to view a capability



                                        $ tput -T hpterm clear | hexdump -C
                                        00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                        00000009


                                        $ tput -T hpterm reset | hexdump -C
                                        00000000 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                        00000010 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 | .1 .1 |
                                        00000020 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 | .1 |
                                        00000030 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 |.1 .1 |
                                        00000040 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 | .1 .1|
                                        00000050 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                        00000060 20 20 1b 31 | .1|
                                        00000064





                                        share|improve this answer



























                                          7












                                          7








                                          7







                                          I'm surprised other answers have not mentioned TERMINFO (or TERMCAP)



                                          Use the man pages Luke



                                          man clear says ...



                                          NAME
                                          clear - clear the terminal screen

                                          SYNOPSIS
                                          clear

                                          DESCRIPTION
                                          clear clears your screen if this is possible. It looks in the environ-
                                          ment for the terminal type and then in the terminfo database to figure
                                          out how to clear the screen.


                                          TERM



                                          The clear command only uses ANSI escape sequences if your $TERM is set to ANSI or some ANSI-based terminal type such as XTERM.



                                          $ TERM=ansi clear | hexdump -C
                                          00000000 1b 5b 48 1b 5b 4a |.[H.[J|
                                          00000006

                                          $ TERM=wy50 clear | hexdump -C
                                          00000000 1b 2b |.+|
                                          00000002

                                          $ TERM=hurd clear | hexdump -C
                                          00000000 1b 63 |.c|
                                          00000002

                                          $ TERM=glasstty clear | hexdump -C
                                          00000000 0c |.|
                                          00000001

                                          $ TERM=vt52 clear | hexdump -C
                                          00000000 1b 48 1b 4a |.H.J|
                                          00000004

                                          $ TERM=hpterm clear | hexdump -C
                                          00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                          00000009


                                          INFOCMP



                                          You can use infocmp to investigate



                                          $ infocmp -L -1 hpterm | grep clear_screen
                                          clear_screen=E&a0y0CEJ,


                                          TPUT



                                          Or you can use tput to view a capability



                                          $ tput -T hpterm clear | hexdump -C
                                          00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                          00000009


                                          $ tput -T hpterm reset | hexdump -C
                                          00000000 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                          00000010 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 | .1 .1 |
                                          00000020 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 | .1 |
                                          00000030 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 |.1 .1 |
                                          00000040 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 | .1 .1|
                                          00000050 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                          00000060 20 20 1b 31 | .1|
                                          00000064





                                          share|improve this answer















                                          I'm surprised other answers have not mentioned TERMINFO (or TERMCAP)



                                          Use the man pages Luke



                                          man clear says ...



                                          NAME
                                          clear - clear the terminal screen

                                          SYNOPSIS
                                          clear

                                          DESCRIPTION
                                          clear clears your screen if this is possible. It looks in the environ-
                                          ment for the terminal type and then in the terminfo database to figure
                                          out how to clear the screen.


                                          TERM



                                          The clear command only uses ANSI escape sequences if your $TERM is set to ANSI or some ANSI-based terminal type such as XTERM.



                                          $ TERM=ansi clear | hexdump -C
                                          00000000 1b 5b 48 1b 5b 4a |.[H.[J|
                                          00000006

                                          $ TERM=wy50 clear | hexdump -C
                                          00000000 1b 2b |.+|
                                          00000002

                                          $ TERM=hurd clear | hexdump -C
                                          00000000 1b 63 |.c|
                                          00000002

                                          $ TERM=glasstty clear | hexdump -C
                                          00000000 0c |.|
                                          00000001

                                          $ TERM=vt52 clear | hexdump -C
                                          00000000 1b 48 1b 4a |.H.J|
                                          00000004

                                          $ TERM=hpterm clear | hexdump -C
                                          00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                          00000009


                                          INFOCMP



                                          You can use infocmp to investigate



                                          $ infocmp -L -1 hpterm | grep clear_screen
                                          clear_screen=E&a0y0CEJ,


                                          TPUT



                                          Or you can use tput to view a capability



                                          $ tput -T hpterm clear | hexdump -C
                                          00000000 1b 26 61 30 79 30 43 1b 4a |.&a0y0C.J|
                                          00000009


                                          $ tput -T hpterm reset | hexdump -C
                                          00000000 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                          00000010 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 | .1 .1 |
                                          00000020 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 | .1 |
                                          00000030 1b 31 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 |.1 .1 |
                                          00000040 20 20 20 20 1b 31 20 20 20 20 20 20 20 20 1b 31 | .1 .1|
                                          00000050 20 20 20 20 20 20 20 20 1b 31 20 20 20 20 20 20 | .1 |
                                          00000060 20 20 1b 31 | .1|
                                          00000064






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Apr 16 '14 at 10:09

























                                          answered Apr 16 '14 at 9:48









                                          RedGrittyBrickRedGrittyBrick

                                          1,6701519




                                          1,6701519





















                                              6














                                              In addition to all nice answer above, we can do some strace to see what happen:



                                              $ strace -e trace=write echo -e "x1bx5bx48x1bx5bx32x4ac"
                                              write(1, "33[H33[2J", 7

                                              $ strace -e trace=write clear
                                              write(1, "33[H33[2J", 7


                                              You can see, two command provide the same ANSI escape sequences.






                                              share|improve this answer



























                                                6














                                                In addition to all nice answer above, we can do some strace to see what happen:



                                                $ strace -e trace=write echo -e "x1bx5bx48x1bx5bx32x4ac"
                                                write(1, "33[H33[2J", 7

                                                $ strace -e trace=write clear
                                                write(1, "33[H33[2J", 7


                                                You can see, two command provide the same ANSI escape sequences.






                                                share|improve this answer

























                                                  6












                                                  6








                                                  6







                                                  In addition to all nice answer above, we can do some strace to see what happen:



                                                  $ strace -e trace=write echo -e "x1bx5bx48x1bx5bx32x4ac"
                                                  write(1, "33[H33[2J", 7

                                                  $ strace -e trace=write clear
                                                  write(1, "33[H33[2J", 7


                                                  You can see, two command provide the same ANSI escape sequences.






                                                  share|improve this answer













                                                  In addition to all nice answer above, we can do some strace to see what happen:



                                                  $ strace -e trace=write echo -e "x1bx5bx48x1bx5bx32x4ac"
                                                  write(1, "33[H33[2J", 7

                                                  $ strace -e trace=write clear
                                                  write(1, "33[H33[2J", 7


                                                  You can see, two command provide the same ANSI escape sequences.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Apr 14 '14 at 19:51









                                                  cuonglmcuonglm

                                                  106k25210308




                                                  106k25210308





















                                                      0














                                                      Firstly /bin/echo -e "x1bx5bx48x1bx5bx32x4ac" doesn't really clear the screen. You can scroll up to see the previous contents.



                                                      Secondly, I opened IRB (which is an interactive Ruby shell), and typed:



                                                      p `clear`


                                                      Or



                                                      p %x(clear)



                                                      Or:



                                                      require 'open3'
                                                      p Open3.capture2('clear')[0]


                                                      All the codes should return "e[3Je[He[2J"



                                                      Now open your terminal, type echo -e "e[3Je[He[2J"



                                                      It should clear the screen. These are called ANSI escape sequences:



                                                      https://en.wikipedia.org/wiki/ANSI_escape_code



                                                      You can use these codes to blink a text (e[5m), colourize a text: (for i in 0..255 ; do printf "e[38;5;$im$i " ; done ; echo) and many other things!






                                                      share|improve this answer



























                                                        0














                                                        Firstly /bin/echo -e "x1bx5bx48x1bx5bx32x4ac" doesn't really clear the screen. You can scroll up to see the previous contents.



                                                        Secondly, I opened IRB (which is an interactive Ruby shell), and typed:



                                                        p `clear`


                                                        Or



                                                        p %x(clear)



                                                        Or:



                                                        require 'open3'
                                                        p Open3.capture2('clear')[0]


                                                        All the codes should return "e[3Je[He[2J"



                                                        Now open your terminal, type echo -e "e[3Je[He[2J"



                                                        It should clear the screen. These are called ANSI escape sequences:



                                                        https://en.wikipedia.org/wiki/ANSI_escape_code



                                                        You can use these codes to blink a text (e[5m), colourize a text: (for i in 0..255 ; do printf "e[38;5;$im$i " ; done ; echo) and many other things!






                                                        share|improve this answer

























                                                          0












                                                          0








                                                          0







                                                          Firstly /bin/echo -e "x1bx5bx48x1bx5bx32x4ac" doesn't really clear the screen. You can scroll up to see the previous contents.



                                                          Secondly, I opened IRB (which is an interactive Ruby shell), and typed:



                                                          p `clear`


                                                          Or



                                                          p %x(clear)



                                                          Or:



                                                          require 'open3'
                                                          p Open3.capture2('clear')[0]


                                                          All the codes should return "e[3Je[He[2J"



                                                          Now open your terminal, type echo -e "e[3Je[He[2J"



                                                          It should clear the screen. These are called ANSI escape sequences:



                                                          https://en.wikipedia.org/wiki/ANSI_escape_code



                                                          You can use these codes to blink a text (e[5m), colourize a text: (for i in 0..255 ; do printf "e[38;5;$im$i " ; done ; echo) and many other things!






                                                          share|improve this answer













                                                          Firstly /bin/echo -e "x1bx5bx48x1bx5bx32x4ac" doesn't really clear the screen. You can scroll up to see the previous contents.



                                                          Secondly, I opened IRB (which is an interactive Ruby shell), and typed:



                                                          p `clear`


                                                          Or



                                                          p %x(clear)



                                                          Or:



                                                          require 'open3'
                                                          p Open3.capture2('clear')[0]


                                                          All the codes should return "e[3Je[He[2J"



                                                          Now open your terminal, type echo -e "e[3Je[He[2J"



                                                          It should clear the screen. These are called ANSI escape sequences:



                                                          https://en.wikipedia.org/wiki/ANSI_escape_code



                                                          You can use these codes to blink a text (e[5m), colourize a text: (for i in 0..255 ; do printf "e[38;5;$im$i " ; done ; echo) and many other things!







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Mar 8 at 10:38









                                                          S.GoswamiS.Goswami

                                                          196




                                                          196



























                                                              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%2f124762%2fhow-does-clear-command-work%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

                                                              How to check contact read email or not when send email to Individual?

                                                              Bahrain

                                                              Postfix configuration issue with fips on centos 7; mailgun relay