ANSI Underline Text - Escape Code 4 for Underlined Output
Add underline styling to terminal text using ANSI escape code 4. Learn to combine underline with colors and other styles. Code examples for all major languages.
Detailed Explanation
Underline Text with ANSI Code 4
Underline is activated with SGR code 4 and reset with code 24. It draws a line under the text, commonly used for hyperlinks, headings, and emphasis in terminal output.
Basic Usage
# Simple underline
echo -e "\033[4mUnderlined text\033[0m"
# Underline with color
echo -e "\033[4;34mBlue underlined link\033[0m"
# Reset only underline (keep other formatting)
echo -e "\033[1;4;32mBold underlined green \033[24mbold green only\033[0m"
Underline Variants (Modern Terminals)
Some modern terminals support extended underline styles:
| Code | Style | Support |
|---|---|---|
| 4 | Single underline | Universal |
| 4:0 | No underline | Limited |
| 4:1 | Single underline | Limited |
| 4:2 | Double underline | Limited |
| 4:3 | Curly underline | Limited |
| 4:4 | Dotted underline | Limited |
| 4:5 | Dashed underline | Limited |
Colored Underlines
Some terminals (Kitty, WezTerm, foot) support colored underlines:
# Set underline color (code 58;2;R;G;B)
echo -e "\033[4;58;2;255;0;0mRed underline\033[0m"
Hyperlinks in Terminals
Modern terminals support clickable hyperlinks using OSC 8:
echo -e "\033]8;;https://example.com\033\\\033[4;34mClick here\033[0m\033]8;;\033\\"
Common Patterns
# Section heading
echo -e "\033[1;4mConfiguration Options\033[0m"
# Clickable-looking link
echo -e "\033[4;36mhttps://example.com\033[0m"
# Emphasized word in a sentence
echo -e "This is \033[4mreally\033[24m important"
Use Case
Underline is commonly used for URLs and hyperlinks in terminal output, section headings in help text, emphasizing specific words or phrases, and creating visual hierarchy in CLI documentation. Man pages use underline extensively for parameter names and file paths.