When you are writing complex, automated bash scripts in Linux, outputting massive blocks of solid white text to the terminal is highly inefficient. If your script encounters a critical error, that error message will be completely buried in the sea of white text. To force the Linux terminal to instantly render specific text strings in bold, red, or green, or to mathematically reposition the cursor exactly in the center of the screen, you must use the tput (Terminal Put) command.
Controlling Text Colors
The tput command interacts directly with the Linux terminfo database, mathematically translating high-level commands into raw escape sequences that the terminal emulator can understand.
To inject color into your bash script output, you must use tput setaf (Set ANSI Foreground) followed by a strict numerical color code (0-7).
- 0: Black
- 1: Red (Critical Errors)
- 2: Green (Success Messages)
- 3: Yellow (Warnings)
- 4: Blue
In a bash script, you execute the tput command immediately before the text you want to format.
tput setaf 1
echo "CRITICAL ERROR: Database Connection Failed!"
tput sgr0
The first line forces the terminal engine to switch its rendering color to solid Red (1). The second line prints your error message. The third line (tput sgr0) is absolutely critical; it mathematically resets the terminal back to its default state. If you forget to include tput sgr0, every single line of text printed after the error will also be permanently rendered in red.
Formatting Text (Bold and Underline)
You can use the exact same logic to alter the structural formatting of the text without changing its color.
To force the terminal to render text in heavy Bold, use tput bold:
tput bold
echo "INSTALLATION COMPLETE"
tput sgr0
To draw a highly visible underline beneath a specific string (perfect for headers), use tput smul (Start Mode Underline), and end it with tput rmul (Remove Mode Underline):
tput smul
echo "Server Status Report"
tput rmul
By combining these commands inside your bash scripts, you can build highly professional, color-coded terminal dashboards that instantly communicate system status without requiring the user to read every single line of output.