Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Color output (ANSI escape codes) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Color output (ANSI escape codes)
Start Script
↓
Define Color Codes
↓
Print Text with Color
↓
Reset Color
↓
End Script
The script starts by defining color codes, then prints colored text, and finally resets the color to normal.
Execution Sample
Bash Scripting
RED='\033[31m'
RESET='\033[0m'
echo -e "${RED}Hello in Red${RESET}"
This script prints 'Hello in Red' in red color and then resets the color back to normal.
Execution Table
StepActionVariable/CodeOutputNotes
1Define RED color codeRED='\033[31m'No outputSets RED to ANSI code for red text
2Define RESET codeRESET='\033[0m'No outputSets RESET to ANSI code to reset color
3Print colored textecho -e "${RED}Hello in Red${RESET}"Hello in Red (in red color)Text appears red in terminal
4End scriptScript endsNo outputColors reset to normal
💡 Script ends after printing colored text and resetting color
Variable Tracker
VariableStartAfter Step 1After Step 2Final
REDundefined\033[31m\033[31m\033[31m
RESETundefinedundefined\033[0m\033[0m
Key Moments - 3 Insights
Why do we need to use the -e option with echo?
The -e option enables interpretation of backslash escapes like \033 for colors, as shown in step 3 of the execution_table.
What happens if we forget to reset the color after printing?
If RESET is not used, the terminal text color stays changed for all following outputs, as implied in step 4 where color reset happens.
Why are the color codes stored in variables instead of used directly?
Storing codes in variables makes the script cleaner and easier to reuse or change colors, as seen in steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what color will the text 'Hello in Red' appear?
AGreen
BRed
CBlue
DDefault terminal color
💡 Hint
Check the value of RED variable defined in step 1 and used in step 3.
At which step is the color reset to normal?
AStep 4
BStep 1
CStep 3
DStep 2
💡 Hint
Look at the notes column in the execution_table for when colors return to normal.
If we remove the -e option from echo in step 3, what happens?
AText prints in red anyway
BScript fails with error
CText prints with literal escape codes visible
DText prints in default color without escape codes
💡 Hint
Refer to the key_moments about why -e is needed for escape code interpretation.
Concept Snapshot
Use ANSI escape codes to color terminal output.
Define color codes as variables like RED='\033[31m'.
Use echo -e to print colored text: echo -e "${RED}text${RESET}".
Always reset color after printing with RESET='\033[0m'.
Without reset, terminal color stays changed.
Full Transcript
This script shows how to print colored text in a terminal using ANSI escape codes. First, it defines RED as the escape code for red text and RESET as the code to reset colors. Then it prints 'Hello in Red' using echo with the -e option to interpret escape codes. Finally, it resets the color so the terminal returns to normal. The execution table traces these steps and the variable tracker shows how RED and RESET are set. Key moments explain why -e is needed and why resetting color is important. The quiz tests understanding of color usage and script behavior.

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

  1. Step 1: Understand ANSI escape code format

    ANSI escape codes start with \e[ and end with m, where numbers define colors or styles.
  2. Step 2: Identify code 31

    Code 31 sets the text color to red in terminal output.
  3. Final Answer:

    Changes the text color to red -> Option C
  4. 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

  1. Step 1: Use correct ANSI escape code format

    The code to start green text is \e[32m and to reset colors is \e[0m.
  2. 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.
  3. Final Answer:

    echo -e "\e[32mSuccess\e[0m" -> Option B
  4. 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

  1. Step 1: Analyze the escape codes in the command

    \e[33m sets yellow text color, \e[0m resets color back to default after 'Warning'.
  2. Step 2: Understand echo -e behavior

    Echo with -e interprets escape sequences, so 'Warning' appears yellow, rest normal.
  3. Final Answer:

    Warning in yellow color followed by ': Disk space low' in default color -> Option D
  4. 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

  1. Step 1: Check echo command usage

    By default, echo does not interpret escape sequences without -e flag.
  2. 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.
  3. Final Answer:

    Missing -e flag in echo to enable escape sequences -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    echo -e "\e[32mSuccess\e[0m"; echo -e "\e[31mError\e[0m" -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard