3

If you do something silly like cat /var/log/wtmp your terminal can get messed up as shown in the screenshot. I know there are a number of ways of fixing this. One of the ways not mentioned on that post, which I was told about years ago is to run the command highlighted in a red box in the screenshot.

head /bin/ls

This works.

Why?

Terminal Reset Screenshot

2 Answers2

7

Terminals are controlled by escape sequences that are sent in-line with the character data to be displayed. That is how, for example, echo -e '\e[34m' will turn the text on many terminals blue. It's echoing some characters to the terminal—they happen to be an escape sequence which sets the foreground color.

The terminal was messed up by being instructed to switch into some alternative character set (and possibly a bunch more things). It did that because /var/log/wtmp happened to contain the escape sequences used to switch character sets. Technically, it's not really messed up—it's operating exactly as it is designed to. (Try tput smacs to mess up your terminal on demand; tput rmacs to change that parameter back.)

reset, etc. function by sending escape sequences resetting various parameters to their defaults. That "fixed" the terminal.

That head /bin/ls trick is working because your /bin/ls (or at least the portion printed by head) happens to contain an escape sequence changing the terminal parameters back. That's not at all portable—it doesn't work here for example—and likely does a much less thorough job resetting defaults than reset, etc.)

derobert
  • 109,670
  • stty sane is at least somewhat more portable, and might do a more complete job of resetting everything. –  Apr 05 '16 at 17:01
  • 3
    @BruceEdiger stty sane doesn't fix tput smacs... but anyway, how to fix it is the other question that the OP linked to. If you want to suggest portable ways to fix it, I suggest posting them as an answer there (if not already present, of course). – derobert Apr 05 '16 at 17:03
3

Actually, the picture does not show any line-drawing characters. Running reset would (usually) help with that. But reset also repairs the terminal modes, somewhat more reliable than stty sane:

  • If you use stty sane, that will fix problems with the terminal modes for echo and carriage return, but on Unix platforms (i.e., AIX, HPUX and Solaris) that sets the erase character to a value which you probably would not want, e.g., ^? or # rather than ^H.
  • The reset command (at least that provided by ncurses) improves on those settings, and sends the terminal initialization string. That usually resets the alternate character set (but some terminals may differ, since this also is a feature sometimes overlooked in Unix terminal descriptions).

If you had shown line-drawing characters, those likely would have been because there was a shift-out control character (not an escape sequence):

SO        Shift Out (Ctrl-N) -> Switch to Alternate Character Set.  This
          invokes the G1 character set.

Here is a screenshot showing the line-drawing characters, which I made by

ls
tput smacs
ls -l

enter image description here

Occasionally someone shows a picture with binary junk mixed with line-drawing characters, but neither this question nor the other mentioned (ab)use that feature. Instead, what they illustrate are two other features:

  • not all characters are printable (consider the large blank areas on the picture), and
  • some byte sequences do not form legal UTF-8, and are shown by terminal emulators with special marks, e.g., the Unicode replacement character.

Further reading:

Thomas Dickey
  • 76,765