How to Use the echo Command in Linux?
The echo command in Linux and Unix prints text, variables, or strings to the terminal (standard output). It’s used to display messages, debug shell scripts, and check variable values.
echo "Hello, World!"
Output: Hello, World!
Introduction
Searching for ways to print a message or display variables in Linux?
Do not worry! It can be done easily using the most powerful echo command, a built-in shell command available in both Linux and Unix systems.
In this tutorial, we’ll guide you through using the echo command in Linux, why it’s helpful, and how to apply it in real shell scripts and terminal sessions. Let’s go through them!
What is echo Command in Linux?
In simple terms, the echo command in Linux is used to print text, variables or characters for the standard output that can be a terminal. Such an in-built Linux command is widely used in shell scripts or terminal commands to display outputs, debug code or system status. The general syntax for the echo command is as follows:
echo [options] [text...]
Here, the [options] signifies varied options for modifying the echo command.
[text….] represents the text or variables to be displayed.
Why Do You Use the echo Command in Linux?
Here are a few reasons you require using the echo command in Linux!
- To showcase text or status in scripts.
- Used for debugging purposes.
- To communicate information with a command-line environment.
- Used for creating user-friendly shell scripts.
- To create complex pipelines and one-liner commands via other tools.
- Display the output of variables on a terminal.
Basic Uses of the echo Command
Let’s explore some of the uses of the echo commands!
1. Display a Message using the echo command.
echo "Hello, World!"
Output: Hello, World!
2. Display the output of variables on a terminal using the echo command.
name="Cantech" && echo "Welcome to $name"
Output: Welcome to Cantech
3. Showcase text message with the system date.
echo "Today is" $(date)
Output: Today is Tue May 6 03:09:30 PM UTC 2025
4. To show output as special characters.
echo -e "Line 1 \nLine 2 \nLine 3"
Output:
Line 1
Line 2
Line 3
5. Display text in a single line.
echo -n "Hello, " echo "eCommerce enthusiasts!"
Output: Hello, eCommerce enthusiasts!
6. Use backspace (\b) to merge words together.
echo -e "This \bis \bour \bwebsite"
Output: Thisisourwebsite
7. To add tabs between the words.
echo -e "This \tis \tmy \twebsite"
Output: This is my website
Useful Options With echo Command
| Option | Description |
| -n | Prevents a newline at the end of the output |
| -e | Enables interpretation of escape characters like \n, \t |
| -E | Disables escape characters (default behavior) |
Escape Sequences to Use with -e
Using echo -e allows you to include special characters in your output. Let’s have a look!
| Escape Sequence | Function |
| \n | New line |
| \t | Tab space |
| \\ | Backslash |
| \” | Double quote |
| \a | Alert (system beep) |
| \b | Backspace |
| \r | Carriage return |
| \c | Suppress remaining output |
Final Thoughts
Using echo command in Linux is simple and widely used in printing simple messages and scripting complicated automation logs. It is the foremost step for beginners to have detailed knowledge of terminal output and powerful shell scripts.
Frequently Asked Questions
Why do you use the echo command in Linux?
echo prints text, strings, or variable values to the terminal (standard output). It’s a core shell builtin used for showing status messages, debugging scripts, and passing output to other commands via pipes.
How do you print a message in Linux?
Use echo followed by your text in quotes:
echo "Hello, World!"
Output: Hello, World!
Double quotes allow variable expansion inside the string; single quotes print text literally.
How do I echo a command path in Linux?
Wrap the command in $() to capture and print its result:
echo (which python3)
Output: /usr/bin/python3
This runs which python3 first, then passes its output to echo.
What is the use of the -n flag?
-n suppresses the trailing newline echo normally adds, so the next output continues on the same line. It’s useful for building prompts or combining multiple echo statements into one line.
Explain the basic difference between echo and printf in Linux.
echo is simple and best for quick output, with basic escape-sequence support via -e. printf offers precise formatting (padding, decimals, alignment) and behaves more consistently across shells.
Is the echo command the same in Unix and Linux?
Yes – echo is a POSIX-standard shell command, so its core syntax and behavior are consistent across Unix, Linux, macOS, and other Unix-like operating systems. You can use the same basic commands (like echo "Hello, World!") on any of these systems and expect identical output. Minor differences do exist between shell implementations – for example, how the -e flag and escape sequences behave can vary slightly between sh, bash, and dash – but for everyday use, echo works the same way everywhere.