9 UNIX / Linux tput Examples: Control Your Terminal Color and Cursor

by Balakrishnan Mariyappan on January 5, 2011

tput command is used to query the terminfo terminal database and check if that terminal supports a specific feature.

tput command accepts the terminal commands and output the control code sequences for that terminal. Using tput you can control the color and cursor of your terminal as explained in this article.

1. Set the Cursor Position using tput cup

You can move the cursor to a specific row and column using tput cup. Following example positions the cursor at row 2 and column 3.

$ tput cup 2 3

2. Clear the Screen Using tput clear

If you are in middle of the terminal screen, tput clear will clear the screen and put you at the top of the terminal screen.

$ tput clear

3. Get the Number of Columns and Lines of a Terminal

To display the number of columns of your terminal screen, do the following.

$ tput cols

Following displays number of lines of your terminal screen.

$ tput lines

4. Execute Multiple tput Commands

tput allows you to run set of commands in a single time. For example, if you want to clear the screen and set cursor to a particular position, do the following.

$ tput -S <<END
> clear
> cup 2 4

5. Change the Terminal Background Color using tput setb

Using tput, the background color of the screen can be changed as shown below.

$ tput setb 4

Note: You can also change only your prompt color using Bash custom prompt PS1.

6. Change the Foreground Color using tput setf

You can also change the foreground color of the terminal as shown below.

$ tput setf 4

Note: If you set foreground and background to the same color you cannot see the cursor. So, to reset, execute “tput reset”.

7. Turn On and Turn Off Highlighting

tput allows you to turn on and turn off the text high lighting. When you turn it ton, new text in the terminal gets bold.

$ tput bold

When you turn it off, new text in the terminal returns to normal display.

$ tput sgr0

In the below example, it bolds the particular text ‘guide’ by turning on and turning off highlighting accordingly.

$ echo `tput bold`guide`tput sgr0`

8. Underline Text using smul and rmul

Start the underline mode:

$ tput smul

Stop the underline mode:

$ tput rmul

In the below example, it underlines the ‘guide’ text by using smul and rmul capabilities of tput.

$ echo `tput smul`guide`tput rmul`

9. Hide and Unhide the Cursor using civis and cnorm

Hide the cursor:

$ tput civis

Display the cursor:

$ tput cnorm

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 7 comments… read them below or add one }

1 Chris F.A. Johnson January 5, 2011 at 3:22 am

On some systems, tput uses termcap not terminfo. On those systems, most of the commands in the article will not work.

The only tput commands recognized by the POSIX standard (meaning they will be found on all systems) are clear, init, and reset.

2 Laxman Rathod January 5, 2011 at 6:35 am

Hi Ramesh, i lkied this article and i like almost all of ur articles they are really very nice and its very easy to understand even for the beginers as u explain with simple example

Thnx a lot some of ur articles have been useful to me in my office work

3 konstantin January 13, 2011 at 6:05 am

well done. thx!!

4 Ethan September 3, 2012 at 6:03 am

good example, but can be more comprehensive.

5 rov October 6, 2012 at 12:19 pm

If the colors don’t work for you, you probably need to use setaf and setab instead of setf/setb. The former uses ANSI escapes.

6 dilip October 21, 2012 at 11:43 am

it’s so useful..
what is the command to bold permanently……….

7 Ali September 27, 2014 at 11:23 am

in my script tput works fine when I run my script in teminal , but tput does not work when that scripts runs in crontab file

Leave a Comment

Previous post:

Next post: