Operations 6 min read

Mastering tput: Control Colors, Cursor, and Text Styles in Linux Terminals

This guide explains how to use the Linux tput command to clear the screen, set foreground and background colors, apply text styles, query terminal dimensions, move the cursor, and create colorful text interfaces, providing practical code examples for each feature.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering tput: Control Colors, Cursor, and Text Styles in Linux Terminals

Clear the Screen

The tput command can clear all text from the terminal. tput clear This leaves the terminal blank and ready for new output.

Set Text Color

Use tput to change the foreground (and background) colors of printed text.

Example – red text:

tput setaf 1
echo "This is red text."
tput sgr0  # reset to default colors
setaf

selects the foreground color; 1 represents red. sgr0 restores the default attributes.

Set Text Style

tput

can also apply styles such as bold or underline.

Example – bold text:

tput bold
echo "This is bold text."
tput sgr0  # reset to default style

Get Terminal Size

Retrieve the number of rows and columns with tput lines and tput cols.

lines=$(tput lines)
cols=$(tput cols)
echo "Terminal has $lines lines and $cols columns."

Move Cursor Position

Position the cursor at a specific row and column using tput cup.

tput cup 5 10
echo "Cursor moved to row 5, column 10."

Hide and Show Cursor

Hide the cursor with tput civis and make it visible again with tput cnorm.

tput civis  # hide cursor

tput cnorm  # show cursor

Check Terminal Color Capability

Determine whether the terminal supports at least eight colors.

if [ "$(tput colors)" -ge 8 ]; then
  echo "This terminal supports color."
else
  echo "This terminal does not support color."
fi

Set Background Color

Change the background color with tput setab. Example – green background:

tput setab 2
echo "This text has a green background."
tput sgr0  # reset colors

Get and Set Text Color Values

Store a color escape sequence in a variable for later reuse.

red_color=$(tput setaf 1)
echo "${red_color}This text is red.${reset_color}"
${reset_color}

should contain the result of tput sgr0 to restore defaults.

Create a Colored Text Interface

Combine multiple tput calls to build a simple colored UI.

# Set colors
title_color=$(tput setaf 4)   # blue
text_color=$(tput setaf 2)    # green
reset_color=$(tput sgr0)      # reset

# Display UI
echo "${title_color}Welcome to My App${reset_color}"
echo "${text_color}This is some important information.${reset_color}"

Conclusion

Understanding the various tput options enables fine‑grained control over terminal output, which is valuable for script development, command‑line interface design, and improving user experience in Linux environments.

Linuxshell scriptingterminaltext formattingcolorstput
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.