📖 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
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
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
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
Hint
Run the script in a terminal that supports ANSI colors to see the red colored message.
Practice
(1/5)
1. What does the ANSI escape code \e[31m do in a bash script?
easy
A. Resets all colors and styles
B. Changes the background color to blue
C. Changes the text color to red
D. Makes the text bold
Solution
Step 1: Understand ANSI escape code format
ANSI escape codes start with \e[ and end with m, where numbers define colors or styles.
Step 2: Identify code 31
Code 31 sets the text color to red in terminal output.
Final Answer:
Changes the text color to red -> Option C
Quick Check:
\e[31m = red text color [OK]
Hint: 31 means red text color in ANSI codes [OK]
Common Mistakes:
Confusing text color with background color
Thinking \e[31m resets colors
Assuming it makes text bold
2. Which of the following is the correct syntax to print the word Success in green color using echo in bash?
easy
A. echo "\e[0m32mSuccess\e"
B. echo -e "\e[32mSuccess\e[0m"
C. echo "\e[32mSuccess"
D. echo "\e[32mSuccess\e[32m"
Solution
Step 1: Use correct ANSI escape code format
The code to start green text is \e[32m and to reset colors is \e[0m.
Step 2: Check each option's correctness
echo -e "\e[32mSuccess\e[0m" correctly wraps 'Success' with start and reset codes. Others have syntax errors or missing reset.
Final Answer:
echo -e "\e[32mSuccess\e[0m" -> Option B
Quick Check:
Start green with \e[32m and reset with \e[0m [OK]
Hint: Always reset color with \e[0m after colored text [OK]
Common Mistakes:
Forgetting to reset color after text
Placing reset code before text
Using incorrect escape code syntax
3. What will be the output of the following bash command?
echo -e "\e[33mWarning\e[0m: Disk space low"
medium
A. No color, plain text output
B. Entire line in yellow color
C. Syntax error due to missing quotes
D. Warning in yellow color followed by ': Disk space low' in default color
Solution
Step 1: Analyze the escape codes in the command
\e[33m sets yellow text color, \e[0m resets color back to default after 'Warning'.
Step 2: Understand echo -e behavior
Echo with -e interprets escape sequences, so 'Warning' appears yellow, rest normal.
Final Answer:
Warning in yellow color followed by ': Disk space low' in default color -> Option D
Quick Check:
\e[33m colors 'Warning', \e[0m resets color [OK]
Hint: Reset color after colored text to limit color scope [OK]
Common Mistakes:
Assuming entire line is colored
Ignoring -e flag in echo
Confusing color codes with styles
4. Identify the error in this bash snippet intended to print Error in red:
echo "\e[31mError\e[0m"
medium
A. Missing -e flag in echo to enable escape sequences
B. Wrong color code for red
C. Reset code \e[0m is incorrect
D. Quotes should be single quotes instead of double
Solution
Step 1: Check echo command usage
By default, echo does not interpret escape sequences without -e flag.
Step 2: Confirm color codes and quotes
Color code 31 for red and reset \e[0m are correct; double quotes allow escape sequences with -e.
Final Answer:
Missing -e flag in echo to enable escape sequences -> Option A
Quick Check:
Use echo -e to interpret \e codes [OK]
Hint: Use echo -e to enable ANSI escape codes [OK]
Common Mistakes:
Forgetting -e flag in echo
Changing correct color codes
Using wrong quote types unnecessarily
5. You want to write a bash script that prints Success in green and Error in red on separate lines, then resets color. Which script snippet correctly does this?
hard
A. echo -e "\e[32mSuccess\e[0m"; echo -e "\e[31mError\e[0m"
B. echo "\e[32mSuccess"; echo "\e[31mError"; echo "\e[0m"
C. echo -e "\e[32mSuccess\e[31mError\e[0m"
D. echo "\e[32mSuccess\n\e[31mError\e[0m"
Solution
Step 1: Check color codes and reset usage
Each line uses correct color code (32 for green, 31 for red) and resets color with \e[0m.
Step 2: Verify echo usage and line separation
echo -e "\e[32mSuccess\e[0m"; echo -e "\e[31mError\e[0m" uses separate echo commands with -e flag and resets color after each line, ensuring correct colors and reset.
Final Answer:
echo -e "\e[32mSuccess\e[0m"; echo -e "\e[31mError\e[0m" -> Option A
Quick Check:
Separate echo with -e and reset color each line [OK]
Hint: Use separate echo -e with reset for each colored line [OK]
Common Mistakes:
Not resetting color after each line
Using single echo without proper newlines
Omitting -e flag in echo
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.