LinuxCommandLibrary

tput

Query terminal capabilities and output escape sequences

TLDR

Move the cursor to a screen location

$ tput cup [row] [column]
copy

Set foreground (af) or background (ab) color
$ tput [setaf|setab] [ansi_color_code]
copy

Reverse text and background colors
$ tput rev
copy

Reset all terminal text attributes
$ tput sgr0
copy

Show number of columns, lines, or colors
$ tput [cols|lines|colors]
copy

Enable or disable word wrap
$ tput [smam|rmam]
copy

Hide or show the terminal cursor
$ tput [civis|cnorm]
copy

Save or restore terminal text status (smcup also captures scroll wheel events)
$ tput [smcup|rmcup]
copy

SYNOPSIS

tput [options] capability

PARAMETERS

capability
    The terminfo capability to invoke or query. Examples include clear (clear screen), cup (cursor position), bold (enter bold mode), cols (number of columns), and lines (number of lines).

-T
    Specifies the terminal type to use instead of the value in the TERM environment variable.

init
    Resets the terminal to its initial state (as defined in the terminfo database).

reset
    Fully resets the terminal, equivalent to 'init', and outputs the reset string.

-S
    Allows multiple capabilities to be specified on the command line, separated by semicolons. This requires careful handling of quoting to prevent shell interpretation of the semicolons.

-V
    Displays the version of the ncurses library.

-x
    Extend format capability string

DESCRIPTION

The tput command utilizes the terminfo database to manipulate terminal settings and output formatted text. It is often used in shell scripts to ensure proper display behavior across different terminal types. tput can perform tasks like clearing the screen, moving the cursor, setting terminal modes (bold, underline, etc.), and querying terminal capabilities. Its functionality relies heavily on the TERM environment variable, which specifies the terminal type. If TERM is not set or refers to an unknown terminal, tput may not work as expected or may produce incorrect results. Proper use of tput allows for portable and robust terminal-based applications that adapt to diverse terminal environments.
It works by consulting the terminfo database, a collection of files that describe the capabilities of various terminals. When you use tput, it reads the terminfo entry for your terminal (as defined by the TERM environment variable) and then outputs the appropriate escape sequences or values.

CAVEATS

The output of tput often consists of terminal escape sequences. These sequences are interpreted by the terminal emulator to perform actions like moving the cursor or changing text attributes. Improper handling of escape sequences can lead to unexpected or undesirable terminal behavior. TERM variable need to be set for correct output.

RETURN CODES

tput returns 0 if the capability is successfully executed or queried. It returns 1 if the capability is unknown or not supported by the terminal. Other non-zero return codes may indicate errors such as incorrect parameter usage.

EXAMPLES

  • Clear the screen: tput clear
  • Move the cursor to row 10, column 20: tput cup 10 20
  • Set bold mode: tput bold
  • Get the number of columns: tput cols

HISTORY

The tput command originated with System V Unix. It's a fundamental tool for controlling terminal behavior in a portable way. It has been included in most Unix-like operating systems since then, evolving as the terminfo database and ncurses library have developed.

SEE ALSO

terminfo(5), curses(3)

Copied to clipboard