tput
Query terminal capabilities and output escape sequences
TLDR
Move the cursor to a screen location
Set foreground (af) or background (ab) color
Reverse text and background colors
Reset all terminal text attributes
Show number of columns, lines, or colors
Enable or disable word wrap
Hide or show the terminal cursor
Save or restore terminal text status (smcup also captures scroll wheel events)
SYNOPSIS
tput [-T type] capname [parameters ...]
tput [-T type] init
tput [-T type] reset
tput [-T type] longname
tput -S
PARAMETERS
-T type
Specifies the type of terminal. If omitted, the value of the TERM environment variable is used. This allows querying capabilities for a terminal other than the current one.
capname
The symbolic name of a terminal capability as defined in the terminfo database. Examples include clear (clear screen), bold (start bold mode), cols (number of columns), lines (number of rows), cup (cursor position).
parameters ...
Numeric parameters required by certain capabilities. For example, cup (cursor position) requires row and column parameters: tput cup row col.
init
Prints the terminal initialization strings. This is often used when a terminal session begins to set up terminal modes.
reset
Prints the terminal reset strings. This is useful for restoring the terminal to a default state, often after an application has left it in an undesirable mode.
longname
Prints the full verbose name for the current terminal type.
-S
Standard mode. Allows reading multiple capability names from standard input, one per line. This is more efficient for batch processing multiple capabilities as it avoids re-initializing the terminfo database for each query.
DESCRIPTION
tput is a utility that allows shell scripts to interrogate the terminfo database and send setup strings to the terminal. It provides a portable way to perform terminal-dependent operations, such as clearing the screen, moving the cursor, changing text attributes (bold, underline, color), or querying terminal dimensions. Instead of embedding hardcoded escape sequences, scripts use tput with symbolic capability names (e.g., clear, bold, cols). tput looks up the corresponding escape sequence or value for the current terminal type (specified by the TERM environment variable or -T option) in the terminfo database and prints it to standard output. This makes scripts more robust and adaptable to different terminal emulators and configurations.
CAVEATS
tput relies entirely on the terminfo database. If the database is incomplete or incorrect for a specific terminal type, tput may not behave as expected or may report unknown capabilities.
The output of tput is raw terminal escape sequences. It should not be displayed directly to the user but rather passed to the terminal for interpretation.
Exit codes: tput returns 0 on success, 1 for unknown capabilities, 2 for usage errors, and 3 for no information about the terminal type. Scripts should check these codes.
USING IN SHELL SCRIPTS
tput is invaluable for making shell scripts portable across different terminal types. Instead of hardcoding escape sequences, scripts can use tput to get the correct sequence for the current terminal. For example, BOLD=$(tput bold), NORMAL=$(tput sgr0), echo "${BOLD}This is bold.${NORMAL}".
EXIT STATUS
The exit status of tput provides important feedback:
0: The capability is valid and printed.
1: The capability is invalid or not defined for the terminal.
2: Usage error.
3: No information about the terminal type (e.g., TERM not set or database entry missing).
HISTORY
tput is part of the ncurses (new curses) project, which is an open-source reimplementation of the System V Release 4.0 (SVr4) curses library. The original tput command was available in System V Unix, providing a command-line interface to the terminfo database. Its development has mirrored that of ncurses itself, focusing on portability, robustness, and extending support for modern terminal features. It superseded older termcap-based utilities due to the more structured terminfo database.