Lesson 6: Input & Output — echo

Goal: Master the echo command for displaying text, working with variables, and creating colored terminal output.


Table of Contents

  1. Basic echo
  2. Quoting — Single vs Double
  3. Special Characters & Escape Sequences
  4. Variables with echo
  5. Writing to Files with echo
  6. Colored Output
  7. Practical Patterns
  8. Exercises

Basic echo

echo prints text to the terminal (standard output).

echo Hello World

Output:

Hello World

With quotes

echo "Hello World"
echo 'Hello World'

Both produce the same output. The difference matters when using variables and special characters.

Without newline

echo -n "Loading..."

This keeps the cursor on the same line (no newline at the end).


Quoting — Single vs Double

Double quotes — variables and escapes are interpreted

name="Benjamin"
echo "Hello, $name!"

Output:

Hello, Benjamin!

Single quotes — everything is literal

name="Benjamin"
echo 'Hello, $name!'

Output:

Hello, $name!

When to use which

Use When
Double quotes "..." You want variables expanded
Single quotes '...' You want literal text (no interpretation)
No quotes Simple words without special characters
Backticks or $(...) Command substitution

Special Characters & Escape Sequences

Use echo -e to enable escape sequences:

echo -e "Line 1\nLine 2\nLine 3"

Output:

Line 1
Line 2
Line 3

Common escape sequences

Sequence Meaning
\n New line
\t Tab
\\ Backslash
\a Alert (bell sound)
\b Backspace
\e Escape character (for colors)

Examples

# Tabs for formatting
echo -e "Name:\tBenjamin"
echo -e "Role:\tDeveloper"
echo -e "City:\tBerlin"

Output:

Name:   Benjamin
Role:   Developer
City:   Berlin
# Multiple newlines
echo -e "Section 1\n\nSection 2\n\nSection 3"

Variables with echo

Environment variables

echo "Home directory: $HOME"
echo "Current user: $USER"
echo "Shell: $SHELL"
echo "Path: $PATH"
echo "Hostname: $HOSTNAME"

Custom variables

greeting="Hello"
target="World"
echo "$greeting, $target!"

Command substitution

Execute a command and use its output:

echo "Today is $(date)"
echo "You are in $(pwd)"
echo "System: $(uname -s)"
echo "Files here: $(ls | wc -l)"

Output:

Today is Sat Mar 22 10:30:00 CET 2026
You are in /home/benjamin
System: Linux
Files here: 15

Arithmetic

echo "2 + 2 = $((2 + 2))"
echo "10 * 5 = $((10 * 5))"
echo "100 / 3 = $((100 / 3))"

Writing to Files with echo

Create / overwrite a file

echo "Hello World" > myfile.txt

Append to a file

echo "New line" >> myfile.txt

Write multiple lines

echo -e "Line 1\nLine 2\nLine 3" > multiline.txt

Practical example: Create a simple script

echo '#!/bin/bash' > hello.sh
echo 'echo "Hello from the script!"' >> hello.sh
echo 'echo "Today is $(date)"' >> hello.sh
chmod +x hello.sh
./hello.sh

Colored Output

You can colorize terminal output using ANSI escape codes.

Basic syntax

echo -e "\e[COLORm Text \e[0m"
  • \e[ — start escape sequence
  • COLORm — color code
  • \e[0m — reset to default

Text colors

echo -e "\e[30m Black \e[0m"
echo -e "\e[31m Red \e[0m"
echo -e "\e[32m Green \e[0m"
echo -e "\e[33m Yellow \e[0m"
echo -e "\e[34m Blue \e[0m"
echo -e "\e[35m Magenta \e[0m"
echo -e "\e[36m Cyan \e[0m"
echo -e "\e[37m White \e[0m"

Bold and styles

echo -e "\e[1m Bold text \e[0m"
echo -e "\e[2m Dim text \e[0m"
echo -e "\e[4m Underlined text \e[0m"
echo -e "\e[5m Blinking text \e[0m"
echo -e "\e[7m Inverted text \e[0m"

Combine style and color

echo -e "\e[1;31m Bold Red \e[0m"
echo -e "\e[1;32m Bold Green \e[0m"
echo -e "\e[4;34m Underlined Blue \e[0m"

Background colors

echo -e "\e[41m Red background \e[0m"
echo -e "\e[42m Green background \e[0m"
echo -e "\e[43m Yellow background \e[0m"
echo -e "\e[44m Blue background \e[0m"

Practical: Status messages

echo -e "\e[1;32m[SUCCESS]\e[0m Operation completed"
echo -e "\e[1;33m[WARNING]\e[0m Disk space low"
echo -e "\e[1;31m[ERROR]\e[0m File not found"
echo -e "\e[1;34m[INFO]\e[0m Starting backup..."

Output (with colors):

[SUCCESS] Operation completed
[WARNING] Disk space low
[ERROR] File not found
[INFO] Starting backup...

Color reference table

Code Color Code Background
30 Black 40 Black
31 Red 41 Red
32 Green 42 Green
33 Yellow 43 Yellow
34 Blue 44 Blue
35 Magenta 45 Magenta
36 Cyan 46 Cyan
37 White 47 White

Practical Patterns

System info banner

echo -e "\e[1;36m================================\e[0m"
echo -e "\e[1;36m   System Information\e[0m"
echo -e "\e[1;36m================================\e[0m"
echo -e "Hostname:\t$(hostname)"
echo -e "User:\t\t$USER"
echo -e "Date:\t\t$(date '+%Y-%m-%d %H:%M')"
echo -e "Uptime:\t\t$(uptime -p)"
echo -e "Kernel:\t\t$(uname -r)"
echo -e "\e[1;36m================================\e[0m"

Simple progress indicator

for i in {1..10}; do
    echo -ne "\rProgress: $i/10"
    sleep 0.5
done
echo -e "\n\e[1;32mDone!\e[0m"

Exercises

Exercise 1: Variables and substitution

# 1. Print your username and home directory
echo "User: $USER, Home: $HOME"

# 2. Print the current date and time
echo "Now: $(date)"

# 3. Print the number of files in /etc
echo "Files in /etc: $(ls /etc | wc -l)"

# 4. Simple arithmetic
echo "365 days = $((365 * 24)) hours"

Exercise 2: Colored output

# 1. Print "Hello" in green
echo -e "\e[32mHello\e[0m"

# 2. Print "Warning" in bold yellow
echo -e "\e[1;33mWarning\e[0m"

# 3. Create a colored status line
echo -e "\e[42m\e[30m STATUS: OK \e[0m"

Exercise 3: Create a file with echo

# 1. Create a simple HTML file
echo '<!DOCTYPE html>' > index.html
echo '<html><body>' >> index.html
echo '<h1>Hello World</h1>' >> index.html
echo '</body></html>' >> index.html

# 2. Verify
cat index.html

# 3. Clean up
rm index.html

Key Takeaways

  • echo "text" — double quotes expand variables
  • echo 'text' — single quotes are literal
  • echo -e — enables escape sequences (\n, \t, colors)
  • echo -n — no trailing newline
  • $(command) — command substitution
  • $((math)) — arithmetic
  • \e[COLORm — ANSI color codes for styled output

Next Lesson: Lesson 7: Users & Permissions →