31

Out of curiosity, when your shell character set breaks from doing something like cat /dev/urandom is there a way to fix that in place?

6
  • 6
    Sure... don't do that! Also, if you think a command might produce invalid characters, use cat -A. Aug 28, 2013 at 13:56
  • 2
    Additionally, I've always been curious how cat can break your character set...
    – crasic
    Aug 28, 2013 at 18:46
  • cat /bin/ls often fixes this (not sure why)
    – skarface
    Aug 28, 2013 at 21:05
  • 2
    @crasic : it sends binary to the terminal, and each terminal have ways to change font/color/etc by receiving special caracters. See the marvelous: linusakesson.net/programming/tty (aka "the TTY demystified). Depending on your TERM setting, it will react differently too (and of course, depending what bytes it receives and in what order). Sep 2, 2013 at 10:53
  • @skarface: you probably got lucky that the binary of /bin/ls had, near the end, byte sequences that fixed the display. It will depend on the OS, the binary (ie, which version, what compile optino used, etc), and the TERM setting of your terminal... so I highly recommend you use "stty sane" instead ^^ Sep 2, 2013 at 10:55

3 Answers 3

51

Try one of these:

stty sane

or

reset

If both don't work, or your terminal is so messed up that you can't even enter commands, then it is best to close the terminal and start a new one.

Note that stty sane is defined by POSIX whereas reset is not. That means on some systems there might not be a reset or it might do something completely different, like resetting the entire system. I have not yet encountered a system without reset.

For more background information read "The Linux keyboard and console HOWTO" chapter "Resetting your terminal".

12
  • I gave you the right answer because you were the first. Sorry to the other ones. Aug 28, 2013 at 12:34
  • In general, writing random bytes to a terminal is unlikely to put it in a situation that needs stty sane to fix; that's more likely to come from a program crashing.
    – Random832
    Aug 28, 2013 at 15:13
  • stty sane is the safe&proper way (I am weary of "reset" for the reasons I stated in comments underneath lain's answer) Aug 29, 2013 at 12:47
  • 1
    @OlivierDulac Thank you for sharing your experience. I researched and learned that reset is not defined by POSIX.
    – Lesmana
    Aug 30, 2013 at 17:25
  • 1
    @Iain I do not think that @OlivierDulac is spreading FUD. The reference that reset is not "safe" is the fact that it is not defined by POSIX. Please stop spreading the FUD that @OlivierDulac is spreading FUD.
    – Lesmana
    Aug 30, 2013 at 17:28
20

You can try using the reset command.

8
  • 4
    You linked the man page, implicitly recommending a good RTFM. You're my hero today, thanks.
    – Luke404
    Aug 28, 2013 at 12:26
  • This works on many unixes... But some word of warning : be careful: on some systems (ie, other OSs, or maybe some other distribution of linux?) this could be initiating a system reset (I believe for example it does on some versions of solaris). Therefore reseting the system, ie forcing a reboot (without proper shutdown)... Like for any command, be careful. (This is true for other commands as well: some 'tar' for example do NOT remove "/" prefixes and therefore can overwrite sensitive files, etc. Always make sure you do know how the command you intend to use work on the system you are using.) Aug 29, 2013 at 12:46
  • 1
    @OlivierDulac; The OP tagged his question linux so he got a linuxcentric answer although it will work on other unix systems including Solaris without issue. I worked with Solaris for 15 years! You are confusing the SPARC Open Boot Prom reset command with the userland reset(1) command. If you issue the reset at the ok prompt you're unlikely to be recovering from a corrupt terminal though.
    – user9517
    Aug 29, 2013 at 14:21
  • @lain: ah, this could be this (Open Boot prom), indeed. But still, it means on some environment "reset" is linked to resetting the host, so I prefer people (even if they are "mostly" on linux) to learn/use the "safe" "stty sane" command (which states what it does, and is not as ambiguous as "reset")... that's why I am warning readers about this. Aug 30, 2013 at 7:22
  • @OlivierDulac Solaris, CentOS, Ubuntu, OpenBSD all link reset to tset - please provide evidence for your misinformation other than your misremembering something. OBP is not a host OS so it doesn't count.
    – user9517
    Aug 30, 2013 at 7:48
11

The reset command should work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .