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 -C00000000  1b 5b 48 1b 5b 32 4a                              |.[H.[2J|00000007and can be replicated with the echo having the exact same effect:$ /bin/echo -e \x1b\x5b\x48\x1b\x5b\x32\x4a\cI was really curious how this output of this command translates to clearing the console.
How does clear command work?
shell;terminal
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};{COLUMN}HSets the cursor position where subsequent text will begin. If no row/columnparameters are provided (ie. <ESC>[H), the cursor will move to the home position,at the upper left of the screen.And:Erase Screen        <ESC>[2JErases 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