Skip to content
Shell & TerminalFix Published Updated 4 min readViews unavailable

Fixing a Terminal That Shows Garbled Output or Invisible Text

After accidentally catting a binary file, the terminal shows strange characters or wrong colors — the shell is fine, the terminal's display state is what broke.

A terminal showing garbled characters, wrong colors, or not echoing typed input after cat-ing a binary file is a terminal emulator display-state problem, not a shell problem — the fix targets the terminal’s settings, not the shell itself.

Step 1: confirm this is actually a display-state issue

If commands still technically execute (even though you can’t see them clearly, or output looks wrong) rather than the shell hanging or refusing input entirely, this is very likely a terminal display/settings problem rather than a shell or process issue.

Step 2: try typing reset blind

reset

Even if you can’t see what you’re typing due to the corrupted display state, typing reset followed by Enter and pressing it works blind — reset reinitializes the terminal to sane default settings and typically clears whatever corrupted state a stray escape sequence left behind.

Step 3: try stty sane as a lighter-weight alternative

stty sane

stty sane restores standard terminal line-discipline settings (echo, line buffering, signal characters) without doing a full terminal reset — often sufficient for less severe corruption, and faster than a full reset.

Step 4: if typing blind doesn’t work, try Ctrl-J instead of Enter

stty sane[Ctrl-J]

If Enter itself isn’t registering correctly due to the corrupted state, Ctrl-J sends a raw linefeed character directly, which can submit the command even when Enter’s normal behavior is affected.

Step 5: close and reopen the terminal tab as a fallback

If neither reset nor stty sane restores normal behavior, closing the specific terminal tab or window and opening a new one is a reliable fallback — since the shell process itself is unaffected by this class of bug, a fresh terminal window connecting to a new shell session resolves it immediately, at the cost of losing that specific session’s state.

Step 6: avoid the root cause going forward

file suspicious-file    # check file type before viewing
less suspicious-file     # less handles binary content more gracefully than cat

Checking a file’s type with file before running cat on it, or using less (which detects and handles binary content more carefully) instead of cat for anything you’re not certain is plain text, avoids triggering this class of corruption in the first place.

Step 7: understand why this specifically affects the display, not the shell

Binary files often contain byte sequences that happen to match terminal escape sequences — commands the terminal emulator interprets as instructions (change color, move cursor, switch character sets) rather than displaying as visible text. cat-ing such a file sends these bytes directly to the terminal, which dutifully executes whatever escape sequences it finds, regardless of whether that was the actual intent.

Why this fix targets the terminal, not the shell, specifically

Since the shell and terminal emulator are separate programs connected via a pty, a corrupted display state lives entirely in the terminal emulator’s rendering and settings — the shell process underneath continues running completely normally, which is exactly why reset (a terminal-directed command) fixes the problem rather than needing to restart the shell or any running process at all.

Why escape sequences have this much power over the display in the first place

The specific control codes a terminal interprets as color, cursor-movement, and character-set instructions trace back to ECMA-48, standardized in 1976 and adopted by ANSI as X3.64 in 1979 — a deliberate, industry-wide replacement for the previously vendor-specific escape sequences every different terminal manufacturer had defined independently. The DEC VT100, released in 1978, was the first widely deployed terminal to implement this standardized set, and the same fundamental sequence format has persisted essentially unchanged through decades of terminal emulator software since, which is exactly why a random binary file’s byte content can still accidentally trigger real display-altering commands today.

Why plain clear doesn’t fix this the way reset does

clear

clear only erases the visible screen content and repositions the cursor to the top — it doesn’t touch the underlying terminal modes (echo, line discipline, character-set selection) that a stray escape sequence can leave in a corrupted state, which is exactly why running clear after cat-ing a binary file often just produces a blank screen that immediately looks wrong again the moment you type anything. reset (or stty sane) specifically targets those underlying mode settings rather than just the visible content, which is the actual distinction that determines which command fixes this particular class of problem.

Related:

Sources:

Comments