Jump to content


1
[Tutorial]

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



1 reply to this topic

#1 spatel

spatel

    Linux TE

  • Technical Expert
  • PipPipPip
  • 322 posts
  • 528 thanks

Posted 06 January 2011 - 03:04 PM

Posted Image

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
> END

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`
guide

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`
guide

9. Hide and Unhide the Cursor using civis and cnorm

Hide the cursor:

$ tput civis

Display the cursor:

$ tput cnorm

Edited by spatel, 06 January 2011 - 03:29 PM.

"The quieter you become, the more you are able to hear..."  - BT

#!/bin/bash
for qw in `cert.txt`
do
echo "Certification passed: $qw"
done

root#./cert.sh
Certification passed: CCNA|RHCE|Solaris|Security+|CEH

Thanked by 1 Member:
sammadu2003

#2 arvindsony

arvindsony

    Member

  • Members
  • PipPip
  • 23 posts
  • 18 thanks

Posted 19 January 2011 - 05:55 AM

thanks dude

Thanked by 1 Member:
sammadu2003



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Organization

Community

Downloads

Test Providers

Site Info


Go to top