0
0
Bash Scriptingscripting~15 mins

Color output (ANSI escape codes) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Color output (ANSI escape codes)
📖 Scenario: You want to make your terminal messages more colorful to catch attention or show status clearly. Using ANSI escape codes, you can add colors to your text output in the terminal.
🎯 Goal: Create a bash script that prints a message in red color using ANSI escape codes.
📋 What You'll Learn
Create a variable holding the ANSI escape code for red color
Create a variable holding the ANSI escape code to reset color
Use these variables to print the message 'Error: File not found!' in red color
Print the message so that the terminal color resets after the message
💡 Why This Matters
🌍 Real World
Colored terminal output helps highlight errors, warnings, or success messages making scripts easier to read and debug.
💼 Career
Many automation and scripting jobs require clear terminal output. Knowing how to add color improves script usability and professionalism.
Progress0 / 4 steps
1
Create the red color variable
Create a variable called RED and set it to the ANSI escape code for red color: \033[31m
Bash Scripting
Need a hint?

The ANSI escape code for red color is '\033[31m'. Assign it as a string to the variable RED.

2
Create the reset color variable
Create a variable called RESET and set it to the ANSI escape code to reset color: \033[0m
Bash Scripting
Need a hint?

The ANSI escape code to reset color is '\033[0m'. Assign it as a string to the variable RESET.

3
Print the colored error message
Use echo -e to print the message 'Error: File not found!' in red color by combining the variables RED and RESET around the message.
Bash Scripting
Need a hint?

Use echo -e to enable interpretation of backslash escapes. Combine ${RED}, the message, and ${RESET} in one string.

4
Display the final colored message
Run the script so that it prints the message Error: File not found! in red color and then resets the terminal color.
Bash Scripting
Need a hint?

Run the script in a terminal that supports ANSI colors to see the red colored message.