When to use echo with -e flag?

QuestionsQuestions8 SkillsProDec, 18 2025
0159

Great question! You use the echo -e flag when you want Linux to interpret special escape characters (codes that start with a backslash \).

By default, echo treats everything inside the quotes as literal text. The -e flag tells echo, "Hey, look for special codes and follow their instructions!"

Here are the most common scenarios for using -e:

1. Adding New Lines (\n)

If you want to print text on separate lines with a single command:

# Without -e: Prints literally "Hello\nWorld"
echo "Hello\nWorld"

# With -e: Prints "Hello" then moves to a new line for "World"
echo -e "Hello\nWorld"

2. Adding Horizontal Tabs (\t)

This is useful for aligning text into columns:

echo -e "Name\tAge\tCity"
echo -e "Labby\t1\tLabEx"

3. Adding Color to your Text

In Linux terminals, you can use specialized "ANSI escape codes" to change text color. For this to work, you must use the -e flag:

# This prints "Hello" in Red text
echo -e "\e[31mHello Red World\e[0m"

(Note: \e[31m starts the red color, and \e[0m resets it back to normal.)

Summary Table

Character Result
\n New line
\t Horizontal tab
\\ Backslash (if you actually want to print one \)

Try it out! Type echo -e "Step 1\nStep 2\nStep 3" into your terminal right now to see how it formats the list! 🚀

0 Comments

no data
Be the first to share your comment!