LinuxCommandLibrary

setterm

Set terminal attributes

SYNOPSIS

setterm [options]

PARAMETERS

-foreground color
    Sets the foreground color of the text. Valid colors include black, red, green, yellow, blue, magenta, cyan, white, and their 'bright' variants (e.g., brightred).

-background color
    Sets the background color of the terminal. Valid colors are the same as for -foreground.

-clear
    Clears the terminal screen and moves the cursor to the top-left corner.

-reset
    Resets the terminal to its initial power-on state, clearing all attributes and screen contents.

-cursor [on|off]
    Controls the visibility of the text cursor. 'on' shows the cursor, 'off' hides it.

-blank [N|force]
    Sets the screen blanking timeout to N minutes. If no activity, the screen blanks. 'force' immediately blanks the screen. Use '0' to disable blanking.

-powersave [on|vsync|hsync|dpms|off]
    Controls VESA power saving features. 'on' or 'dpms' enables DPMS, 'off' disables it. 'vsync' and 'hsync' control vertical and horizontal sync respectively.

-msg [on|off]
    Enables or disables the display of kernel messages on the console.

-dump [N]
    Dumps the contents of virtual console N (or current console if N is omitted) to standard output.

-ulcolor color
    Sets the color for underlined text. Only applicable if the terminal supports underline colors.

-halfbright [on|off]
    Enables or disables the half-bright attribute for characters.

-blength N
    Sets the duration of the bell sound in milliseconds.

-bfreq N
    Sets the frequency of the bell sound in Hz.

-store
    Saves the current terminal settings to memory.

-recall
    Restores the terminal settings previously saved with -store.

-regtabs N
    Sets tab stops at every N columns.

-term terminal_name
    Sets the terminal type for the current console, for compatibility with programs that check TERMCAP or TERMINFO.

DESCRIPTION

The setterm command is a utility used to configure various attributes of a Linux console (TTY). It allows users and scripts to control aspects like foreground and background colors, cursor visibility, screen blanking, bell parameters, and other display characteristics. Unlike generic terminal manipulation tools like tput which rely on the terminfo database, setterm directly interacts with the kernel's terminal driver using specific escape sequences, making it particularly suited for the virtual consoles provided by the Linux kernel. It is commonly employed in shell scripts to set up specific terminal environments, for example, to change prompt colors, clear the screen, or reset the terminal to a known state after an application exits.

CAVEATS

setterm is primarily designed for direct Linux console (TTY) interaction. Its functionality may be limited or behave unexpectedly when used within graphical terminal emulators (e.g., xterm, gnome-terminal, konsole), as these emulators often implement their own display logic or rely on different escape sequences. Some options may require root privileges or specific kernel support. Effects are typically session-specific and revert upon closing the terminal or rebooting.

USAGE IN SHELL SCRIPTS

setterm is widely used in shell scripts for various purposes, such as:
Setting specific foreground and background colors for shell prompts (e.g., via PS1 environment variable).
Clearing the screen before displaying an application's output.
Temporarily hiding the cursor during sensitive operations.
Ensuring a consistent terminal state across different script executions.
For example, setterm -foreground green -clear could be used at the start of a script.

INTERACTION WITH KERNEL TTY DRIVER

Unlike generic terminal control programs that rely on terminfo or termcap databases (like tput), setterm directly communicates with the Linux kernel's virtual console (TTY) driver. It achieves this by writing specific control sequences and escape codes, which the kernel interprets to modify the console's behavior and appearance. This direct interaction is what makes it so effective for the bare Linux console environment.

HISTORY

The setterm command is part of the util-linux project, a fundamental collection of utilities for the Linux operating system. It has been a standard tool for managing Linux console attributes for many years, providing a simpler interface to kernel terminal drivers than manually emitting complex ANSI escape sequences. While its direct use might be less frequent in modern graphical desktop environments, it remains essential for server administration, embedded systems, and shell scripting on bare Linux consoles.

SEE ALSO

tput(1), stty(1), reset(1), console_codes(4), terminfo(5)

Copied to clipboard