Explore `echo` command with different escape sequences.

QuestionsQuestions8 SkillsProDec, 11 2025
0108

You got it! Exploring echo with escape sequences is a great way to see its power for formatting output.

When you use the -e option with echo, it enables the interpretation of backslash escapes. This means certain special character combinations (called escape sequences) will be translated into actions rather than just being printed as plain text.

Let's try some common escape sequences. Type these into your terminal one by one and observe the output:

  1. New Line (\n): This inserts a line break.

    echo -e "First line.\nSecond line."

    You should see "First line." on one line and "Second line." on the next.

  2. Tab (\t): This inserts a horizontal tab space.

    echo -e "Column 1\tColumn 2\tColumn 3"
    echo -e "Item A\t\tValue X\t\tDetail 1"
    echo -e "Item B\t\tValue Y\t\tDetail 2"

    This helps align text into columns. Notice how two \t are sometimes needed for alignment if the previous text is short.

  3. Backslash (\\): If you need to print a literal backslash, you have to escape it with another backslash.

    echo -e "This is a backslash: \\"
  4. No Newline (\c): This is a bit special. When \c is encountered, echo stops processing and outputs no further characters, and crucially, does not print a final newline character.

    echo -e "This text will be followed by more on the same line\c and this will be on the same line."

    You'll notice that "and this will be on the same line." appears right after the first part without a line break. Be careful with this one, as it can make your prompt appear on the same line as the command output!

  5. Return (\r): This moves the cursor to the beginning of the current line, allowing subsequent characters to overwrite what was there.

    echo -e "Long text that will be overwritten\rShort text"

    You should see "Short textthat will be overwritten". "Short text" overwrites the beginning of "Long text", but the rest of the original long text remains.

  6. Bell (\a): This produces an audible alert (a "bell" sound) from your terminal if your system is configured to do so.

    echo -e "Attention! \a"

    You might hear a beep!

Important Note: Remember, you must use the -e option for these escape sequences to be interpreted. If you omit -e, echo will simply print \n, \t, etc., as literal text.

Experiment with these! Try combining a few of them. This is how you really start to master command-line tools. Feel free to show me what you come up with or if you have any questions about specific escape sequences!

0 Comments

no data
Be the first to share your comment!