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
|
|
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
|
|
Output:
|
|
This is the most basic usage.
Example 2: Display Variable Contents
|
|
Output:
|
|
Often used to check what’s inside a variable.
Example 3: Display Without Newline
|
|
Output:
|
|
The -n option suppresses the newline.
Example 4: Using Escape Sequences
|
|
Output:
|
|
The -e option enables escape sequences like \n (newline).
Example 5: Insert Tabs
|
|
Output:
|
|
\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’t1 2 3NAME="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
1echo -
Multiple strings: You can specify multiple strings separated by spaces
1 2echo one two three # Output: one two three -
Redirect to file: Combine with
>or>>to write to files1echo "Log message" >> log.txt
Practical Usage
Display Messages in Shell Scripts
|
|
Useful for showing script progress.
Debug Variables
|
|
I often use this to check variable contents.
Append to File
|
|
Can be used to log records to a file.
Colored Output (Advanced)
|
|
You can add colors using escape sequences (a bit advanced).
Summary
In this article, we learned about the echo command.
Key Points:
echois a basic command for displaying strings- Convenient for checking variable contents
- Use
-eoption 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!