Linux echo Command
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.
Display Text
This example demonstrates how to display a simple text message.
echo "Hello, World!"
The echo command outputs the text Hello, World! to the terminal.
Display Variable Values
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.
Redirect Output to a File
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.
Append Output to a 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.
Escape Special Characters
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.
Suppress Newline
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.
Best Practices for echo
- Use for Debugging: Use
echoto 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
echowith commands likegreporsedfor advanced text processing.
Source
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.
Author
List all Linux tutorials.