Tput
Tput
The tput command allows shell scripts to do things like clear the screen, underline text, and center
text no matter how wide the screen is. To do these things, it translates the terminal-independent
name of a terminal capability into its actual value for the terminal type being used.
tput takes as an argument the name of a Unix System V terminfo capability, which it translates into
the equivalent termcap capability name (see section Capabilities, for a list of the equivalencies).
Terminfo is a database that is similar to termcap but which has different capability names and is
stored in a different format. The GNU tput command takes a terminfo name as an argument to make
it compatible with the Unix System V tput command.
There are three types of terminfo (and termcap) capabilities: string, Boolean, and numeric. String
capabilities either cause a special effect on the terminal when they are displayed or are the value
sent by a special key on the terminal (the latter type are probably of no use in shell scripts). Numeric
and Boolean capabilities give information about the terminal such as how many columns wide it is or
whether whether it has a meta key. See section Output and Exit Status, for more detailed
information on the three types of capabilities.
[-t] [--termcap]
[-S] [--standard-input]
[-V] [--version]
tput clear
Using tput
[-t] [--termcap]
[-S] [--standard-input]
[-V] [--version]
Some string capabilities accept parameters, such as the number of lines to delete or the column to
move to. These parameters are specified on the command line following the capability name. They
are always numbers.
`-T termtype'
`--terminal=termtype'
This option indicates the type of terminal. By default, this value is taken from the `TERM'
environment variable.
`-t'
`--termcap'
GNU tput by default accepts termcap name if the capability a user specifies is not a terminfo name.
This option tells tput not to try terminfo names and look up only termcap names.
`-S'
`--standard-input'
This option tells tput to read a sequence of capabilities and parameters from the standard input.
Only string capabilities can be used in this mode.
`-V'
`--version'
When `longname' is specified, tput displays the long descriptive name for the terminal type.
When `init' is specified, tput sends the initialization strings for the terminal. If the terminal's tab
width is other than 8, and it cannot be reset, the tab expansion in the tty driver is turned on.
Otherwise, tab width is reset to 8 and the tab expansion is turned off.
When `reset' is specified, tput sends the reset strings for the terminal and then follows the same
initialization sequence as tput `init'.
Below are some example uses of tput. See section Capabilities, for a complete list of the functions
that tput can cause terminals to perform. Note that not all terminals can perform any given function.
See section Yet More Examples, for some more complex samples of tput use.
The following command moves the cursor to row 10, column 30 of the screen:
tput cup 10 30
tputcivis
tputcnorm
The following command deletes 10 lines below and including the one on which the cursor is
positioned:
tput dl 10
The tput command produces different kinds of output for each of the three types of terminal
capabilities: string, numeric, and Boolean.
If the terminfo capability given on the command line is a string capability, tput displays its value and
exits with a status of 0. If the capability is not defined for the terminal type being used, tput
produces no output and exits with a status of 1.
If the capability is a numeric capability, tput displays its value (an integer). If the capability is not
defined for the terminal type being used, tput displays the value `-1'. The exit status is always 0 for
numeric capabilities, unless an error occurs (see section Error Messages for a complete list of the
possible exit status values).
If the capability is a Boolean capability, tput produces no output and exits with status 0 if the
capability is defined for the terminal type being used, or status 1 if the capability is not defined. See
section `Definitions of the Terminal Capabilities' in Termcap, for a more detailed description of
termcap capabilities.
The values of numeric capabilities should be saved into shell variables so they can be used later
without having to run tput again. Here is how it can be done:
The values of string capabilities can be saved in shell variables in the same way, then displayed later
using the echo command. Since echo is built into most shells, it runs more quickly than tput does.
However, using echo instead of tput to display string values can cause problems for capabilities that
use padding, because null padding characters cannot be passed as arguments to commands,
including echo.
Here are some more advanced examples of using tput; most involve some shell programming.
Because the C shell's flow control (decision making) constructs differ from those of the other shells,
these examples do not work under the C shell.
The following sequence of commands prints `I am infalible' and then crosses it out on terminals that
can overstrike, and prints `I am on strike' on terminals that cannot.
if tputos; then
else
echo 'I am on strike'
fi
The following example is a shell script that centers a line of text given as command line arguments.
An alternative approach would be to have tput send the `rep' terminfo capability to print the
multiple spaces instead of using the while loop.
}'
The following commands cause the terminal to save the current cursor position, print `Hello, World'
centered in the screen in reverse video, then return to the original cursor position.
COLUMNS=`tput cols`
LINES=`tput lines`
line=`expr $LINES / 2`
column=`expr \( $COLUMNS - 6 \) / 2`
tputsc
tput rev
tput sgr0
tputrc
The middle three lines of the above example can also be written using `--standard-input'.
sc
rev
EOF
Capabilities
1. Boolean Capabilities
Name TermcapDescription
Equiv.
osos Overstrikes
Name TermcapDescription
Equiv.
3. String Capabilities
In the following table, `(P)' following an explanation means that the capability takes one or more
parameters (and is evaluated by the tparam function, or in the case of `cup', tgoto); `(*)' means that
padding may be based on the number of lines affected; and `#n' refers to the `n'th parameter.
NameTermcapDescription
Equiv.
khomekhHome key
Error Messages
tput displays various error messages if problems occur. In addition, it exits with one of the following
status values:
The terminal type given (either in the `TERM' environment variable or by the `-T' switch) is unknown,
or the termcap database can not be read.