Shell echo Command
echois a built-in Shell command used to display a line of text or the value of a variable on standard output (usually the terminal).
Shell's echo command is similar to PHP's echo command; both are used for string output.
Command format:
echo [选项] [字符串]
Why do you need echo?
- Information feedback: Display script execution status or results to the user
- Debugging tool: Output variable values or execution position to help debug scripts
- Interactive interface: Create a simple user interaction interface
- File generation: Quickly generate configuration files or scripts
Basic Usage
1. Simple text output
The most basic usage is to directly output a string:
Example
Execution result:
Hello, World!
2. Output variables
echoYou can display the value of a variable:
Example
echo "Welcome, $name!"
Execution result:
Welcome, Linux User!
3. Output without quotes
Quotes are not required, but it is recommended to use them to avoid accidents:
Example
Execution result:
This is a test
Common Options
-n option: output without newline
By default,echoadds a newline after the output. Using-ncan prevent this behavior:
Example
echo " Done!"
Execution result:
Loading... Done!
-e option: enable escape character interpretation
Enable interpretation of backslash escapes:
Example
Execution result:
First line Second line
Common Escape Sequences
| Escape sequence | Description |
|---|---|
\n |
Newline |
\t |
Horizontal tab |
\v |
Vertical tab |
\b |
Backspace |
\r |
Carriage return |
\\ |
Backslash character itself |
Advanced Usage
1. Output to file
Use redirection to save output to a file:
Example
Append content to a file:
Example
2. Colored output
Use ANSI escape codes to implement colored text:
Example
echo -e "\033[42;31mGreen Background with Red Text\033[0m"
Color code reference:
- Foreground colors: 30 (black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (purple), 36 (cyan), 37 (white)
- Background colors: 40-47 correspond to the above colors
\033[0mReset all attributes
3. Output command execution result
Use command substitution to output command results:
Example
Execution result:
Today is Wed Jul 12 14:30:22 CST 2023
Practical Application Examples
1. Create a simple menu
Example
echo "1. Check disk space"
echo "2. List running processes"
echo "3. Show system info"
echo -n "Please enter your choice [1-3]: "
2. Progress bar simulation
Example
for i in {1..20}; do
echo -n "#"
sleep 0.1
done
echo "] Done!"
3. Generate configuration file
Example
# Generated by script on $(date)
[Database]
host = localhost
port = 3306
user = appuser
password = secret123
EOF
Notes
1. The importance of quotes:
Example
echo $var # May have problems if the variable contains spaces
echo "$var" # Correct way
2. Implementation differences between different shells:
- Bash built-in
echoSupports-eoption - On some systems,
/bin/echomay not support all options - For portable scripts, it is recommended to use
printfto replace complexecho
3. Special character handling:
Example
echo "Path: /usr/local/bin" # Slashes do not need escaping
Summary Points
| Key point | Description |
|---|---|
| Basic syntax | echo [选项] [字符串] |
| Common options | -nNo newline,-eEnable escapes |
| Variable output | Use$variable name, it is recommended to wrap in double quotes |
| Colored output | Use ANSI escape codes\033[XXm |
| Output redirection | >Overwrite file,>>Append to file |
| Multi-line output | Use\nnewline or Here Document |
| Portability recommendations | For complex output, consider usingprintf |