Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท System Information

Linux tput command

Reference for tput: query terminal size and capabilities, set colors and bold text, move the cursor and write portable scripts using terminfo.

tput asks the terminfo database what a terminal can do and either prints the answer (a number such as the width) or the escape sequence that performs an action (bold, red text, move the cursor). Using tput instead of hard-coded escape codes keeps scripts working on terminals that differ from the one you wrote them on.

The command takes a capability name such as cols, bold or setaf, plus arguments if the capability needs them. Which terminal it consults comes from the TERM environment variable.

Terminal
tput [-T TYPE] CAPABILITY [ARGUMENT ...]

Try it

Bash Numbers and strings from terminfo

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash A portable colored message

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
tput [-T type] capname [parameters...]
tput [-T type] -S            # read several capabilities from standard input
tput [-T type] init | reset  # initialize or reset the terminal
Name Description
cols, lines Terminal width and height. When a real terminal is attached tput queries it; otherwise it uses COLUMNS/LINES or the terminfo default.
colors Number of colors the terminal supports (8, 16, 256 and so on).
setaf N, setab N Set foreground or background color number N (ANSI order: 0 black, 1 red, 2 green, 3 yellow, 4 blue, 5 magenta, 6 cyan, 7 white).
bold, dim, smul, rmul, rev Turn on bold, dim, underline start, underline end, reverse video.
sgr0 Turn off all attributes (colors, bold, underline).
cup ROW COL Move the cursor to a row and column (counting from 0).
clear, el, ed Clear the screen, clear to end of line, clear to end of screen.
civis, cnorm Hide the cursor, show it again.
sc, rc Save and restore the cursor position.
-T type Use the named terminal type instead of $TERM.
-S Read capability lines from standard input and print the combined result.

Returns: 0 on success. For boolean capabilities the status is 0 for "true" and 1 for "false"; for an unknown capability it is 4 on modern ncurses, and 2 or 3 for usage errors or a missing terminal type.

Errors and edge cases: when TERM is unset, tput prints "No value for $TERM and no -T specified" and fails. With output redirected to a pipe or file, cols and lines cannot ask the terminal, so values may come from the environment instead.

Usage notes

  • Use capability names, not infocmp codes. tput accepts both terminfo names (setaf) and old termcap names (AF), but setaf is portable across systems.
  • Always end with sgr0. Without it the color keeps applying to every later line, including your shell prompt after the script finishes.
  • Check for a terminal. Test [ -t 1 ] before emitting sequences to a log file or a pipe. Some scripts use tput only when stdout is a TTY and export NO_COLOR support for users who turned colors off.
  • Cost. Each tput call is a process start. In loops, call it once into a variable and reuse it (as in the second example).
  • Cursor movement (cup, civis, sc/rc) is useful for progress bars and menus: print, move back and overwrite.
  • Differences from raw escape codes. \e[31m works on nearly every modern terminal, but tput also covers old and unusual terminals. For bash prompts wrap sequences in \[ \], and in PS1 use \[$(tput setaf 2)\] to keep the line-length calculation correct.
  • Related command. clear is essentially tput clear with a scrollback option. stty size prints rows and columns directly from the tty.
  • The database. infocmp $TERM shows every capability of a terminal; toe -a lists known types.