Linux tutorial on the echo command, covering basic and advanced text display with practical examples.
last modified March 3, 2025
The echo command in Linux is used to display text or variables to the terminal. It is a simple yet powerful tool for printing messages, debugging scripts, and manipulating text. This tutorial covers basic and advanced usage of echo with practical examples.
echo is commonly used for printing text, displaying variable values, and redirecting output to files.
This example demonstrates how to display a simple text message.
echo “Hello, World!”
The echo command outputs the text Hello, World! to the terminal.
This example shows how to display the value of a variable.
name=“John” echo “Hello, $name”
The echo command outputs Hello, John by substituting the value of the name variable.
This example demonstrates how to redirect the output of echo to a file.
echo “This is a test” > testfile.txt
The > operator redirects the output to testfile.txt, creating or overwriting the file.
This example shows how to append text to an existing file.
echo “This is another line” >> testfile.txt
The >> operator appends the output to testfile.txt without overwriting its contents.
This example demonstrates how to display special characters like <, >, and &.
echo “Use < and > for redirection.”
The echo command outputs the text with special characters properly escaped.
This example shows how to suppress the newline character at the end of the output.
echo -n “No newline here”
The -n option prevents echo from adding a newline at the end of the output.
Use for Debugging: Use echo to print variable values and debug scripts.
Redirect Output: Use > and >> to create or modify files.
Escape Special Characters: Use proper escaping to display special characters.
Combine with Other Commands: Use echo with commands like grep or sed for advanced text processing.
In this article, we have explored various examples of using the echo command for displaying text, variables, and special characters, including advanced features like output redirection and suppressing newlines.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Linux tutorials.