Chapter 9: Bash Echo (echo)

Bash Echo (echo) — the echo command, one of the most used and most helpful commands in Bash.

Think of echo like your personal loudspeaker in the terminal. Whatever you want to say (text, variable value, date, result of command), you tell echo and it shouts it on the screen for you to see.

It’s super simple at first, but has many powerful tricks — colors, new lines, tabs, writing to files, no extra newline, etc. We’ll go slow, step-by-step, like we’re practicing together in Hyderabad.

What does “echo” do exactly?

  • Prints text or values to the standard output (your terminal screen)
  • Adds a newline (\n) at the end by default (so next command starts on new line)
  • Used everywhere: interactive typing, scripts for messages, debugging, creating files, prompts, etc.

Basic syntax:

Bash

1. Super Basic – Just Print Text

Bash

Output:

text

Notice: space between words is preserved, and it adds a newline at end automatically.

With quotes (very common — safer!):

Bash

→ Both print the line + newline.

2. Print Variables (Very Important!)

Variables are shown with $

Bash

Output:

text

Without quotes → can break if variable has spaces:

Bash

Best: always double-quote when using variables:

Bash

Output example:

text

3. Important Options (The Real Power)

Option What it does Example
-n No trailing newline (print on same line) echo -n “Loading… “
-e Enable escape sequences (\n, \t, etc.) echo -e “Line1\nLine2”
-E Disable escapes (default in some shells) Rarely needed

Most useful combo: echo -e + echo -n

4. Escape Sequences with -e (Special Formatting Magic!)

When you use -e, Bash understands these backslash codes:

Escape Meaning Example Command What you see
\n New line echo -e “Hi\nHow are you?” Hi How are you?
\t Horizontal tab echo -e “Name:\tWebliance” Name: Webliance
\r Carriage return (overwrite line) echo -e “Loading\rDone!” Done! (overwrites Loading)
\b Backspace (remove previous char) echo -e “Geeks\bFor\bGeeks” GeeksForGeeks
\ Literal backslash echo -e “Path is C:\\Windows” Path is C:\Windows
Literal double quote echo -e “He said \”Hello\”” He said “Hello”
\a Alert (beep sound) echo -e “\aAttention!” Beep + Attention!
\e Escape character (for colors) Used in ANSI colors (next section)

Real example:

Bash

Output:

text

Nice table-like!

5. No Newline + Prompt Style (Very Common in Scripts)

Bash

→ Cursor stays on same line after question — looks professional.

6. Writing to Files (Super Useful!)

echo + redirection:

Bash

Now cat myfile.txt shows:

text

Multi-line with one echo:

Bash

7. Colors in Terminal (Fun & Professional!)

Use ANSI escape codes with \e or \033

Bash
  • \e[0m → reset color
  • Common codes: 31=red, 32=green, 33=yellow, 34=blue, 1=bold

Example nice status:

Bash

8. Quotes – Single vs Double (Quick Rule)

  • Double quotes “ → variables and $(commands) expand
  • Single quotes ‘ → everything literal, no expansion
Bash

9. Quick Practice Session (Do Now!)

Bash

Summary Table – Echo at a Glance

What you want Command Example
Simple text echo “Hello”
Variable echo “Hi $name”
No newline echo -n “Prompt: “
New line / tab echo -e “Hi\nThere\tTabbed”
Write to file (overwrite) echo “Data” > file.txt
Append to file echo “More” >> file.txt
Color (green) echo -e “\e[32mGreen\e[0m”
Beep sound echo -e “\a”
Suppress newline in script echo -n “…”

Got it, boss? echo is like the Swiss Army knife of output — simple but endless uses in scripts!

Any part confusing? Want more on colors table? Or “how to make echo wait 2 seconds” or next command like “read input”?

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *