Set Terminal Attributes Safely with setterm
Your terminal is stuck in reverse video because a script called setterm and never cleaned up after itself. This guide is a small setterm workflow: change terminal presentation and console behaviour, check what the command actually emitted, and restore the normal rendering defaults. The examples use /usr/bin/setterm from util-linux 2.39.3, matching the installed setterm(1) manual on this machine.
The route
Jump straight to the step you need, or tick off Done means at the end.
- Time: about ten minutes.
- You need: a shell and a terminal that understands the capabilities you ask for.
- Scope: most examples are ordinary user commands that write control sequences to standard output; they do not edit a configuration file. Virtual-console-only options need a real Linux virtual console, not an arbitrary terminal emulator.
1. Check which setterm you are running
There can be more than one copy on a development machine, so check the path and version before relying on its behaviour:
$ command -v setterm
/home/linuxbrew/.linuxbrew/bin/setterm
$ /usr/bin/setterm --version
setterm from util-linux 2.39.3
The first result is host-specific. If it is not the binary you intend to use, call /usr/bin/setterm explicitly in scripts or correct your PATH. The local manual documents util-linux 2.39.3 and says long options with two hyphens have been supported since version 2.25; it still recommends the single-hyphen form in scripts for compatibility, though the examples below use the clearer double-hyphen form interactively.
Checkpoint
Run /usr/bin/setterm --help. The output should identify the command as setting terminal attributes and list options such as --foreground, --cursor, --clear and --default.
2. Change a reversible display attribute
Set the foreground colour to green for the current terminal:
$ /usr/bin/setterm --foreground=green
$ printf '%s\n' 'This text should be green'
The first command normally prints a short terminal control sequence rather than a report. A terminal emulator interprets it, and the text that follows appears in the selected colour. Redirect it to a file instead and you capture those bytes rather than changing the terminal display:
$ /usr/bin/setterm --foreground=green > /tmp/setterm-colour.seq
$ wc -c /tmp/setterm-colour.seq
5 /tmp/setterm-colour.seq
That byte count is an observation from this installed environment, not a portable output contract: terminfo and the terminal type determine the sequence. Do not print a captured sequence into a log or paste it into an untrusted terminal without treating it as control data.
Restore the normal rendering settings before leaving a shared session:
$ /usr/bin/setterm --default
This is the undo step for the colour and rendering examples above. If a terminal still looks wrong, /usr/bin/setterm --reset emits the terminal reset string, a stronger reset that may clear more state.
3. Use cursor, wrapping and clearing deliberately
These options alter the current terminal session, so keep them visible in a script and put a recovery command right beside them:
$ /usr/bin/setterm --cursor=off
$ /usr/bin/setterm --linewrap=off
$ /usr/bin/setterm --cursor=on
$ /usr/bin/setterm --linewrap=on
--cursorcontrols whether the cursor is displayed.--linewrapcontrols whether a full line continues on a new one.- Boolean options accept
onoroff; the manual says the default for boolean options is on. - The final two commands above restore the usual enabled state.
Be careful with clearing. --clear with no argument, or with --clear=all, clears the whole screen and homes the cursor; --clear=rest clears from the cursor to the end. Neither operation deletes files, but either can erase useful terminal output. Treat it as a display-disrupting action, not a harmless status command.
4. Change console-only settings only on a virtual console
Options such as --repeat, --msg, --msglevel, --blank, --tabs and --regtabs target Linux virtual-console behaviour, and the manual marks them virtual-consoles-only. A terminal emulator usually cannot provide those capabilities, and setterm may ignore an unsupported option or report that the terminal does not support it.
For example, this is a tab query with no requested positions, run from an ordinary terminal:
$ /usr/bin/setterm --tabs
setterm: terminal dumb does not support --tabs
The exact diagnostic depends on TERM and the terminal. On a real virtual console, --tabs with no arguments shows the current tab settings; --regtabs=8 clears the tab stops and creates a regular eight-column pattern. Do not use these options in a service or remote shell expecting them to configure every terminal.
Likewise, --blank=10 changes the inactivity interval in minutes, while --blank=0 disables automatic blanking. --blank=force keeps a virtual console blank after a key press, and --blank=poke unblanks it. These affect the active console and can interrupt someone else working there, so get agreement before changing them. The undo for a ten-minute setting is --blank=0, but verify the host's desired policy first.
5. Separate terminal type from privilege
setterm consults terminfo where it can. If TERM is wrong, the generated sequence can be wrong too, so inspect it before forcing an override:
$ printf 'TERM=%s\n' "$TERM"
TERM=xterm-256color
$ /usr/bin/setterm --term=xterm-256color --foreground=cyan
--term overrides the environment for this invocation only; it does not change the shell or system configuration. Use your actual terminal type rather than copying this placeholder value.
Do not reach for sudo for ordinary colour, cursor or reset operations. Elevated privileges are not a general fix for an unsupported terminal. Virtual-console operations may need access to the active console, but confirm you are on the intended console and understand the effect first. The command writes terminal control data; it does not grant permission to change another user's terminal.
6. Use scripts without confusing output and status
setterm's useful result is often the escape sequence, not human-readable output. Check its exit status and keep diagnostic output separate:
if /usr/bin/setterm --foreground=default > /dev/tty; then
printf '%s\n' 'terminal rendering restored'
else
status=$?
printf 'setterm failed with status %s\n' "$status" >&2
exit "$status"
fi
Only replace /dev/tty when the process has a specific controlling terminal; a service without a terminal should not pretend it changed a user's display. For a script that must preserve an existing terminal state, avoid broad resets and document exactly which attributes it changes: setterm gives you no general transaction or automatic rollback.
Done means
- You confirmed the binary path and util-linux version.
- You used a supported terminal type and treated emitted sequences as control data.
- You restored display changes with
--default, or used the documented undo for a console timer. - You kept virtual-console-only options away from ordinary terminal emulators.
- You know which commands clear or disrupt the display and did not run them blindly.