Posted on October 10, 2023 by nexonhost
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.
Flags
In addition to the capability names, the following strings are supported as arguments to the tput subroutine.
Item | Description |
---|---|
clear | Displays the clear screen sequence (this is also a capability name). |
init | Displays the sequence that initializes the user’s terminal in an implementation-dependent manner. |
reset | Displays the sequence that will reset the user’s terminal in an implementation-dependent manner. |
longname | Displays the long name and the specified terminal (or current terminal if none specified). |
-S | 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. |
-TType | Indicates the type of terminal. If -T is not specified, the TERM environment variable is used for the terminal. |
Exit Status
This command returns the following exit values:
Item | Description |
---|---|
0 | The requested string was written successfully. |
1 | Unspecified. |
2 | Usage error. |
3 | No information is available about the specified terminal type. |
4 | The specified operand is invalid. |
>4 | An error occurred. |
Installation
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:
tput --version
If you see a version number, you’re ready to proceed.
Basic Usage
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:
# Get the number of columns tput cols # Get the number of rows tput lines
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:
tput cup 0 0
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:
# Set text color to red tput setaf 1 # Set background color to yellow tput setab 3 # Reset text attributes to default tput sgr0
4. Clearing the Terminal
The tput command can be used to clear the terminal screen, making it ready for new output:
tput clear
Advanced Usage
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:
tput setaf 1 # Set text color to red tput setab 3 # Set background color to yellow echo "Hello, Linux!" tput sgr0 # Reset text attributes
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:
#!/bin/bash while true; do tput civis # Hide cursor echo -n "Loading..." sleep 1 tput cuu1 # Move cursor up one line tput el # Clear the line echo -n " " sleep 1 done
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:
# Get the number of columns cols=$(tput cols) # Get the number of rows lines=$(tput lines) echo "Terminal size: ${cols} columns x ${lines} rows"
Advanced Text Formatting
1. Text Styles
Tput allows you to apply various text styles to your output, such as bold, underline, and blink:
# Apply bold text style tput bold # Apply underline text style tput smul # Apply blink text style tput blink
To disable these styles, you can use the sgr0 attribute to reset text attributes to their default values:
tput sgr0
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:
text="Centered Text" cols=$(tput cols) text_length=${#text} position=$(( (cols - text_length) / 2 )) tput cup 0 $position echo "$text"
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:
#!/bin/bash clear echo "Menu:" echo "1. Option 1" echo "2. Option 2" echo "3. Quit" echo -n "Enter your choice: " read choice case $choice in 1) echo "You selected Option 1." ;; 2) echo "You selected Option 2." ;; 3) echo "Goodbye!" exit 0 ;; *) echo "Invalid choice." ;; esac
2. Capturing Keystrokes
You can capture specific keypresses using tput. For example, to detect the pressing of the Enter key:
#!/bin/bash echo "Press Enter to continue..." tput smkx # Enable keypad transmit mode read -s -N1 key tput rmkx # Disable keypad transmit mode if [ "$key" == $'\x0a' ]; then echo "You pressed Enter." else echo "You pressed a different key." fi
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:
#!/bin/bash clear tput setaf 3 # Set text color to yellow echo "Menu:" tput setaf 2 # Set text color to green echo "1. Option 1" echo "2. Option 2" tput setaf 1 # Set text color to red echo "3. Quit" tput setaf 7 # Reset text color to default echo -n "Enter your choice: " read choice case $choice in 1) echo "You selected Option 1." ;; 2) echo "You selected Option 2." ;; 3) echo "Goodbye!" exit 0 ;; *) echo "Invalid choice." ;; esac
Conclusion
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.