ANSI Reset Codes - How to Reset Terminal Formatting
Complete reference for ANSI reset codes including the universal reset (0), and specific resets for bold (22), italic (23), underline (24), color (39/49), and other attributes.
Detailed Explanation
ANSI Reset Codes
Reset codes restore terminal attributes to their default state. Using proper resets prevents color and style bleeding into subsequent terminal output.
The Universal Reset (Code 0)
Code 0 clears all formatting attributes at once:
echo -e "\033[1;31;4mStyled text\033[0m Normal text"
This is the most commonly used reset and should be appended to the end of every styled output.
Specific Reset Codes
| Code | Resets | Counterpart |
|---|---|---|
| 0 | Everything | All codes |
| 22 | Bold and Dim | 1, 2 |
| 23 | Italic | 3 |
| 24 | Underline | 4 |
| 25 | Blink | 5 |
| 27 | Reverse | 7 |
| 28 | Hidden | 8 |
| 29 | Strikethrough | 9 |
| 39 | Foreground color | 30-37, 90-97, 38;5;N, 38;2;R;G;B |
| 49 | Background color | 40-47, 100-107, 48;5;N, 48;2;R;G;B |
Why Use Specific Resets?
When you want to change one attribute while keeping others:
# Remove underline but keep bold and color
echo -e "\033[1;4;31mBold underline red\033[24m Bold red only\033[0m"
# Change foreground color but keep background
echo -e "\033[31;42mRed on green \033[34mBlue on green\033[0m"
# Remove all colors but keep formatting
echo -e "\033[1;4;31mBold underline red \033[39;49mBold underline default\033[0m"
Common Mistakes
Forgetting to reset:
# BAD - color bleeds to the next line
echo -e "\033[31mRed text"
echo "This will also be red!"
# GOOD - always reset
echo -e "\033[31mRed text\033[0m"
echo "This is normal"
Resetting too broadly:
# BAD - loses the background color
echo -e "\033[42m\033[1mBold \033[0mno longer green background"
# GOOD - only reset bold
echo -e "\033[42m\033[1mBold \033[22mnormal weight, still green\033[0m"
Reset at Script Exit
Always reset terminal state when your script exits:
trap 'printf "\033[0m"' EXIT
This ensures the terminal returns to its normal state even if the script crashes or is interrupted.
Use Case
Proper reset code usage is critical for any production CLI tool. Without resets, colored output can corrupt the user's terminal session, making subsequent commands unreadable. This is especially important in tools that pipe output, run in CI/CD environments, or are used within other shell scripts where unexpected formatting can cause parsing issues.