How To Use Tput Command In Linux.
The tput command is a versatile and powerful tool in the Linux command-line arsenal. It is primarily used for manipulating and querying terminal capabilities, such as cursor control, text formatting, and color manipulation. In this article, we will explore the tput command in detail, providing explanations and practical examples to illustrate its various capabilities.
In addition to the capability names, the following strings are supported as arguments to the tput subroutine.
Displays the clear screen sequence (this is also a capability name).
Displays the sequence that initializes the user’s terminal in an implementation-dependent manner.
Displays the sequence that will reset the user’s terminal in an implementation-dependent manner.
Displays the long name and the specified terminal (or current terminal if none specified).
Uses stdin. This allow the tput to process multiple capabilities. When using the -S option, the capabilities cannot be entered on the command line. Enter ^D token finished.
Indicates the type of terminal. If -T is not specified, the TERM environment variable is used for the terminal.
This command returns the following exit values:
The requested string was written successfully.
No information is available about the specified terminal type.
The specified operand is invalid.
An error occurred.
The tput command is typically pre-installed on most Linux distributions, so you can start using it right away without any additional installation steps. To check if tput is available on your system, open a terminal and type:
If you see a version number, you’re ready to proceed.
1. Query Terminal Capabilities
One of the primary uses of tput is to query terminal capabilities. You can use it to retrieve information about various terminal attributes, such as the number of columns and rows in the terminal:
2. Cursor Movement
Tput allows you to move the cursor to specific positions within the terminal. For instance, to move the cursor to the top-left corner (0,0), you can use:
3. Text Formatting
You can also use tput to apply text formatting, such as setting text colors, background colors, and text styles. Here are some examples:
4. Clearing the Terminal
The tput command can be used to clear the terminal screen, making it ready for new output:
1. Creating Colorful Output
You can use tput to create colorful and visually appealing output in your terminal scripts. For instance, to print a message with red text on a yellow background:
2. Animation and Dynamic Output
Tput can be used in shell scripts to create dynamic and animated terminal output. For example, you can create a simple loading animation:
3. Terminal Size Handling
You can use tput to adjust your terminal output based on the terminal’s size. This is particularly useful in shell scripts that need to adapt to different screen resolutions:
Advanced Text Formatting
1. Text Styles
Tput allows you to apply various text styles to your output, such as bold, underline, and blink:
To disable these styles, you can use the sgr0 attribute to reset text attributes to their default values:
2. Text Alignment
You can align text within the terminal using tput. For example, to center-align a text message within the terminal, you can calculate the center position and move the cursor accordingly:
Handling Keyboard Input
1. Reading User Input
Tput can also be used in conjunction with other shell commands to create interactive scripts that accept user input. For instance, you can create a simple menu system:
2. Capturing Keystrokes
You can capture specific keypresses using tput. For example, to detect the pressing of the Enter key:
Creating Colorful Menus
Tput can be used to create colorful menus and interactive interfaces. Here’s an example of a simple menu with colored options:
The tput command is a versatile tool that can be used for a wide range of tasks in the Linux terminal. Whether you’re formatting text, manipulating the cursor, creating colorful output, handling user input, or designing interactive menus, tput provides the flexibility and control you need to enhance your command-line scripts and applications. By mastering its capabilities, you can create more sophisticated and user-friendly terminal-based experiences.