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:
|
0 1 2 3 4 5 6 |
echo [options] [string or words...] |
1. Super Basic – Just Print Text
|
0 1 2 3 4 5 6 |
echo Hello Hyderabad! |
Output:
|
0 1 2 3 4 5 6 |
Hello Hyderabad! |
Notice: space between words is preserved, and it adds a newline at end automatically.
With quotes (very common — safer!):
|
0 1 2 3 4 5 6 7 |
echo "Namaste from Bash class!" echo 'Single quotes also work!' |
→ Both print the line + newline.
2. Print Variables (Very Important!)
Variables are shown with $
|
0 1 2 3 4 5 6 7 8 9 |
name="Webliance" city="Hyderabad" echo "Hi, I am $name from $city!" |
Output:
|
0 1 2 3 4 5 6 |
Hi, I am Webliance from Hyderabad! |
Without quotes → can break if variable has spaces:
|
0 1 2 3 4 5 6 |
echo Hi I am $name from $city # works but risky |
Best: always double-quote when using variables:
|
0 1 2 3 4 5 6 |
echo "Today is $(date)" # $( ) runs command and inserts result |
Output example:
|
0 1 2 3 4 5 6 |
Today is Wed Feb 25 13:30:45 IST 2026 |
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:
|
0 1 2 3 4 5 6 |
echo -e "Item1\tPrice\nApple\t₹50\nBanana\t₹30" |
Output:
|
0 1 2 3 4 5 6 7 8 |
Item1 Price Apple ₹50 Banana ₹30 |
Nice table-like!
5. No Newline + Prompt Style (Very Common in Scripts)
|
0 1 2 3 4 5 6 7 8 |
echo -n "What is your name? " read name echo "Hello $name, welcome!" |
→ Cursor stays on same line after question — looks professional.
6. Writing to Files (Super Useful!)
echo + redirection:
|
0 1 2 3 4 5 6 7 8 |
echo "This is line 1" > myfile.txt # create/overwrite echo "Line 2" >> myfile.txt # append echo "Line 3" >> myfile.txt |
Now cat myfile.txt shows:
|
0 1 2 3 4 5 6 7 8 |
This is line 1 Line 2 Line 3 |
Multi-line with one echo:
|
0 1 2 3 4 5 6 |
echo -e "Line A\nLine B\nLine C" > report.txt |
7. Colors in Terminal (Fun & Professional!)
Use ANSI escape codes with \e or \033
|
0 1 2 3 4 5 6 7 8 9 |
echo -e "\e[1;31mRed Bold Text\e[0m Normal again" echo -e "\e[1;32mGreen Success!\e[0m" echo -e "\e[1;33mYellow Warning\e[0m" echo -e "\e[1;34mBlue Info\e[0m" |
- \e[0m → reset color
- Common codes: 31=red, 32=green, 33=yellow, 34=blue, 1=bold
Example nice status:
|
0 1 2 3 4 5 6 |
echo -e "\e[1;32m✓ Backup completed successfully\e[0m" |
8. Quotes – Single vs Double (Quick Rule)
- Double quotes “ → variables and $(commands) expand
- Single quotes ‘ → everything literal, no expansion
|
0 1 2 3 4 5 6 7 |
echo "Today: $(date)" # works → shows date echo 'Today: $(date)' # literal → prints $(date) |
9. Quick Practice Session (Do Now!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
echo "Hello from Hyderabad!" echo -n "Your city: " echo "Hyderabad" echo -e "Line 1\nLine 2\tTabbed" echo "My home is $HOME" echo -e "\e[1;32mSuccess!\e[0m Check above" echo "Log entry" >> ~/my_log.txt cat ~/my_log.txt |
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”?
