Shell echo Command
echo is 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 to output strings.
Command format:
echo [选项] [字符串]
Why do we need echo?
- Information feedback: show the user the script's execution status or results
- Debugging tools: output variable values or execution positions 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 output a string directly:
Example
echo
"Hello, World!"
Execution result:
Hello, World!
2. Outputting variables
echo You can display the value of a variable:
Example
name
=
"Linux User"
echo
"Welcome, $name!"
Execution result:
Welcome, Linux User!
3. Output without quotes
Quotes are not required, but their use is recommended to avoid surprises:
Example
echo
This is a
test
Execution result:
This is a test
Common options
-n option: output without a trailing newline
By default,echo A newline is added after the output. Use -n to suppress this behavior:
Example
echo
-n
"Loading..."
echo
" Done!"
Execution result:
Loading... Done!
-e option: enable interpretation of escape characters
Enable interpretation of backslash escapes:
Example
echo
-e
"First line\nSecond line"
Execution result:
First line
Second line
Common escape sequences
| Escape sequences | Description |
|---|---|
\n |
Newline |
\t |
Horizontal tab |
\v |
Vertical tab |
\b |
Backspace |
\r |
Carriage return |
\\ |
The backslash character itself |
Advanced usage
1. Output to a file
Use redirection to save output to a file:
Example
echo
"This will be saved to file"
output.txt
Append content to a file:
Example
echo
"Additional line"
output.txt
2. Colored output
Use ANSI escape codes for colored text:
Example
echo
-e
"\033[31mRed Text\033[0m"
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 colors above
\033[0mReset all attributes
3. Output the result of a command
Use command substitution to output a command's result:
Example
echo
"Today is $(date)"
Execution result:
Today is Wed Jul 12 14:30:22 CST 2023
Practical Application Examples
1. Create a simple menu
Example
echo
-e
"\n\033[1mSystem Menu\033[0m"
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
echo
-n
"Progress: ["
for
i
in
{
1
..
20
}
;
do
echo
-n
"#"
sleep
0.1
done
echo
"] Done!"
3. Generate a configuration file
Example
cat
<<
EOF
|
sudo
tee
/
etc
/
myapp.conf
Generated by script on $(date)
[
Database
]
host = localhost
port =
3306
user = appuser
password = secret123
EOF
Notes
1. The importance of quotes:
Example
var
=
"Hello World"
echo
$var
May cause problems if a variable contains spaces
echo
"$var"
Right approach
2. Differences between Shell implementations:
- Bash built-in
echoSupported-eOption - On some systems,
/bin/echomay not support all options - Portable scripts should use
printfinstead of complexecho
3. Handling special characters:
Example
echo
"Cost: $100"
Output the $ symbol
echo
"Path: /usr/local/bin"
Slashes do not need escaping
Summary of key points
| Key points | Description |
|---|---|
| Basic Syntax | echo [选项] [字符串] |
| Common options | -n no newline,-e enable escapes |
| variable output | Use $变量名, it is recommended to wrap it in double quotes |
| Color Output | use ANSI escape codes \033[XXm |
| Output redirection | > overwrite the file,>> append to a file |
| Multiline output | Use \n newline or Here Document |
| Portability recommendations | For complex output consider using printf |