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?

200_success
  • 4,701
  • 1
  • 24
  • 42
  • 6
    Sure... don't do that! Also, if you think a command might produce invalid characters, use `cat -A`. – We Are All Monica Aug 28 '13 at 13:56
  • 2
    Additionally, I've always been curious how `cat` can break your character set... – crasic Aug 28 '13 at 18:46
  • cat /bin/ls often fixes this (not sure why) – skarface Aug 28 '13 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: http://www.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). – Olivier Dulac Sep 02 '13 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 ^^ – Olivier Dulac Sep 02 '13 at 10:55
  • @crasic: http://en.wikipedia.org/wiki/ANSI_escape_code shows examples of those caracters (and, as they say in that very page, "most terminal emulators interpret at least some of the ANSI escape sequences in the output text." (ie, not all term emulators, and not all those codes)) – Olivier Dulac Sep 02 '13 at 12:11

3 Answers3

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".

Lesmana
  • 2,284
  • 2
  • 17
  • 12
  • I gave you the right answer because you were the first. Sorry to the other ones. – Pedro Montoto García Aug 28 '13 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 '13 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) – Olivier Dulac Aug 29 '13 at 12:47
  • @OlivierDulac: Please provide a reference for your belief that reset isn't safe. – user9517 Aug 29 '13 at 14:24
  • @Iain: it's not that : it's that on some OSs (from memory I had the experience on an old Solaris, in production) it could be that "reset" is not to reset the terminal, but to do a soft reset of the host, therefore bypassing the shutdown (and not doing at all the "terminal reset" you expected if you come from the recent linux world)... So i'd recommand to stick to "stty sane" to not get in the habit of typing "reset", that's all (even though on linux it will reset the terminal as expected, on other OSs it could, indeed, reset the system) – Olivier Dulac Aug 30 '13 at 07:19
  • @OlivierDulac You are plain wrong: You are spreading FUD. – user9517 Aug 30 '13 at 07:20
  • @lain: Like I said, I prefer people to use the safe one (stty sane) instead of one that, in some environment it may not be doing the "terminal reset". It *did* happen to me, that's why I wanted to add that warning. I had a garbled putty, issued the "reset" and had the host rebooted... call it FUD? I call this "being prudent, and warning to use caution with some commands". – Olivier Dulac Aug 30 '13 at 07:50
  • @OlivierDulac Being prudent is reading the documentation; unsubstantiated claims like yours are FUD. – user9517 Aug 30 '13 at 15:18
  • 1
    @OlivierDulac Thank you for sharing your experience. I researched and learned that `reset` is not defined by POSIX. – Lesmana Aug 30 '13 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 '13 at 17:28
  • @lesmana until Oliver can prove that his _experience_ is not a poor memory of an experience I'll call FUD on it. – user9517 Aug 30 '13 at 21:36
  • @lain: why so upset? I am just warning people to prefer 'stty sane' to 'reset' as the later, in some environment, could do something else (up to reseting the system). You pointed out yourself what could have been my experience on solaris, and I acknowledged it: it was probably at openBoot (years ago, details are blurred. But, being new to openboot, and having connected via serial to the machine itself, I expected a "reset" would either fix the display (if already loggued in) or say a garbled "inccorect login/pwd" (if at the login prompt) but it reset the system instead (as I was in the OBP). – Olivier Dulac Sep 02 '13 at 10:49
20

You can try using the reset command.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • 4
    You linked the man page, implicitly recommending a good RTFM. You're my hero today, thanks. – Luke404 Aug 28 '13 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.) – Olivier Dulac Aug 29 '13 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 '13 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. – Olivier Dulac Aug 30 '13 at 07: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 '13 at 07:48
  • @lain: OBP does count. In my experience (see my last reply to the top answer), with a garbled display, I couldn't tell I was at the OBP level instead of in the console or already loggued in. Had I tried "stty sane" the system wouldn't have rebooted (nor been fixed, of course). I will stop replying now, but I wish you wouldn't be so upset when I am just trying to warn people (this is NOT FUD). – Olivier Dulac Sep 02 '13 at 10:58
  • @OlivierDulac if you had tried stty sane at the OBP it wouldn't have recognised the command. FUD is FUD – user9517 Sep 02 '13 at 17:38
  • @lain: ... and wouldn't have rebooted. My point, exactly. – Olivier Dulac Sep 03 '13 at 08:33
11

The reset command should work.

Changaco
  • 880
  • 5
  • 10