0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.