echo — Linux basic command
Linux / Basic Linux commands / echo.html
A shell builtin command that prints text to the screen or saves it to a file.
Basic Information
| Command Name | echo |
|---|---|
| One-Line Summary | A shell builtin command that prints text to the screen or saves it to a file. |
| Provided By | Bash/Zsh shell builtin (pre-installed in all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/echo.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL (shell builtin) |
| Installation Required | Pre-installed (no separate installation needed) |
| Category | Essential Linux Command (Tags: text, output, redirection, linux, basic) |
What is this command?
echo is the most basic shell builtin command that prints text given as arguments to the screen. When used with redirection (>, >>), it can save text to a file. It can also print the values of environment variables.
It is widely used in shell scripts for displaying messages, creating files, and checking variable values.
What problems does it solve?
- Prints simple text to the screen.
- Saves text to a file.
- Checks the value of environment variables.
- Displays progress messages in shell scripts.
Main Features
Text Output
Prints text given as arguments to the screen.
- Usage Scenario: When you need to display a simple message or result.
- Result: The input text is printed as-is.
- Limitations: For complex formatting, using printf is recommended.
File Saving (>, >>)
Saves or appends text to a file using redirection.
- Usage Scenario: When you need to quickly create a file or append content.
- Result: The text is saved to the file.
- Limitations: > overwrites existing content, so caution is needed.
Printing Environment Variables
Prints variable values using the $ symbol.
- Usage Scenario: When you need to check the value of environment or shell variables.
- Result: The current value of the variable is printed.
- Limitations: Undefined variables are printed as empty strings.
Options (-n, -e)
-n omits the newline; -e enables escape character processing.
- Usage Scenario: When you need output without a newline or want to use escape sequences.
- Result: Output is formatted as desired.
- Limitations: Escape character processing behavior may vary depending on the Bash version.
Installation and Execution
Check Built-in Status
It is built into all shells.
type echo
# echo is a shell builtin
Usage in Shell Scripts
It is used within scripts to display messages.
#!/bin/bash
# Simple script example
echo "Starting deployment..."
sleep 1
echo "Deployment complete."
Practical Examples
Basic Text Output
Prints text to the screen.
# Basic output
echo "Hello, World!"
# Output without newline
echo -n "Processing... "
# Multiple words (quotes not required)
echo Hello World
Save to File
Saves text to a file.
# Create file (overwrite)
echo "First line" > file.txt
# Append to file
echo "Second line" >> file.txt
# Append multiple lines
echo -e "line1\nline2\nline3" >> file.txt
# Check contents
cat file.txt
Print Environment Variables
Checks variable values.
# Print PATH variable
echo $PATH
# Current user
echo $USER
# Current directory
echo $PWD
# Custom variable
echo "Server: $SERVER_NAME"