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
Comments on this entry are closed.
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.
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
well done. thx!!
good example, but can be more comprehensive.
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.
it’s so useful..
what is the command to bold permanently……….
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
how can we highlight specific “word” in text file?
Example: read<test.txt
This is test file to find word. This word will be highlighted.
Lets learn how to highlight word.
Here i would like to highlight "word" in test.txt file.
I am not getting tput bold and underline command in my terminal