echo Command
The echo command displays text to the terminal or redirects it to files. It's one of the most fundamental commands in Linux, commonly used for printing messages, displaying variables, and creating output in shell scripts.
Syntax
Description
The echo command outputs the given strings to standard output, followed by a newline. It's essential for shell scripting, displaying messages, showing variable values, and creating simple text output.
Key features:
- Display text and messages
- Print variable values
- Support escape sequences
- Redirect output to files
- Built-in shell command
Common Options
| Option | Description |
|---|---|
-n |
Do not output trailing newline |
-e |
Enable interpretation of backslash escapes |
-E |
Disable interpretation of backslash escapes (default) |
Escape Sequences (with -e option)
| Sequence | Description |
|---|---|
\\ |
Backslash |
\a |
Alert (bell) |
\b |
Backspace |
\c |
Suppress trailing newline |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
Examples
Display simple text
Outputs: Hello, World!
Display without quotes
Outputs: Hello World
Display variables
Displays the username and a formatted message
Display without trailing newline
Prompts for input without moving to next line
Use escape sequences
Outputs three lines using newline escape sequence
Tab-separated output
Creates tab-separated columns
Display special characters
Demonstrates bell sound and backslash display
Multiple arguments
Combines text with command output
Redirect to file
Creates and appends to a file
Display environment variables
Shows common environment variables
Colorized output (with ANSI codes)
Displays colored text using ANSI escape codes
Create simple progress indicator
Creates a simple progress indicator
Display array elements
Shows how to display array contents
Common Use Cases
- Shell Scripting: Display messages and status updates
- Variable Display: Show environment variables and script variables
- File Creation: Create simple text files with content
- Debugging: Print debug information in scripts
- User Interaction: Display prompts and messages
- Configuration: Generate configuration files
Shell Scripting Examples
Example shell script using echo for information display