Introduction

Hello! In this article, we’ll learn about the echo command.

echo is a simple command that just displays text, but it’s used incredibly frequently. When writing shell scripts, you’ll almost certainly use it.

What is the echo Command?

echo is a command that displays specified strings on the screen.

You might think “That’s it?”, but this is actually quite useful. You can display messages, check variable contents, write text to files, and more.

Basic Syntax

1
echo [options] [string]

Main Options

Option Description
-n Don’t output the trailing newline
-e Enable interpretation of backslash escapes
-E Disable interpretation of backslash escapes (default)

Usage Examples

Example 1: Basic Usage

1
echo "Hello, World!"

Output:

1
Hello, World!

This is the most basic usage.

Example 2: Display Variable Contents

1
2
NAME="Taro"
echo "Hello, $NAME!"

Output:

1
Hello, Taro!

Often used to check what’s inside a variable.

Example 3: Display Without Newline

1
2
echo -n "No newline here"
echo "Continued here"

Output:

1
No newline hereContinued here

The -n option suppresses the newline.

Example 4: Using Escape Sequences

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

Output:

1
2
3
Line 1
Line 2
Line 3

The -e option enables escape sequences like \n (newline).

Example 5: Insert Tabs

1
echo -e "Name:\tTaro\nAge:\t25"

Output:

1
2
Name:	Taro
Age:	25

\t inserts a tab.

Common Escape Sequences

Symbol Meaning
\n Newline
\t Tab
\\ Backslash
\" Double quote

※ Requires the -e option

Tips & Notes

  • Quote usage: Double quotes (") expand variables, but single quotes (') don’t

    1
    2
    3
    
    NAME="Taro"
    echo "Hello, $NAME!"  # Taro is displayed
    echo 'Hello, $NAME!'  # $NAME is displayed as-is
    
  • Output blank line: Calling echo without arguments outputs a blank line

    1
    
    echo
    
  • Multiple strings: You can specify multiple strings separated by spaces

    1
    2
    
    echo one two three
    # Output: one two three
    
  • Redirect to file: Combine with > or >> to write to files

    1
    
    echo "Log message" >> log.txt
    

Practical Usage

Display Messages in Shell Scripts

1
2
3
4
#!/bin/bash
echo "Starting process..."
# Some work
echo "Process completed!"

Useful for showing script progress.

Debug Variables

1
2
3
#!/bin/bash
FILE_PATH="/home/user/file.txt"
echo "Debug: FILE_PATH = $FILE_PATH"

I often use this to check variable contents.

Append to File

1
echo "$(date): Backup completed" >> backup.log

Can be used to log records to a file.

Colored Output (Advanced)

1
2
echo -e "\e[32mThis is green\e[0m"
echo -e "\e[31mThis is red\e[0m"

You can add colors using escape sequences (a bit advanced).

Summary

In this article, we learned about the echo command.

Key Points:

  • echo is a basic command for displaying strings
  • Convenient for checking variable contents
  • Use -e option for newlines and tabs
  • Frequently used in shell scripts

Simple but seriously high frequency command. It’s essential level for writing shell scripts, so definitely get comfortable with it.

Keep learning Linux commands!