Linux echo command
The echo command is one of the most basic and commonly used commands in Linux, used to display text or the value of variables in the terminal.
echo It is a built-in command in Linux/Unix systems, mainly used for:
- Displaying text information in the terminal
- Outputting the value of variables
- Generating formatted strings
- Appending content to a file
Basic Syntax
echo [选项] [字符串]
**Common Options: **
- -n Do not output a trailing newline
- -e Enable interpretation of escape characters
- -E Disable interpretation of escape characters (default)
Escape characters (require the -e option):
\n- Newline\t- Tab\r- Carriage return\b- Backspace\\- Backslash\"- Double quote\a- Bell
Basic Usage
Simple Text Output
The most basic usage is to output a string directly:
echo "Hello, World!"
Execution result:
Hello, World!
Output variables
echo You can display the value of a variable:
name="Linux User"
echo "Welcome, $name!"
Execution result:
Welcome, Linux User!
Output Without Quotes
Quotes are not required, but their use is recommended to avoid problems with special characters:
echo This is a test
Execution result:
This is a test
Details of common options
The -n option: Suppress 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!
The -e option: Enable Escape Characters
Enable interpretation of backslash escape characters:
Example
echo
-e
"First line\nSecond line"
Execution result:
First line
Second line
Common escape characters:
\n: Newline\tTab character\\Backslash\aAlert (bell)
Output colored text
You can output colored text by combining escape characters:
Example
echo
-e
"\033[31mRed Text\033[0m"
echo
-e
"\033[42;30mGreen Background\033[0m"
Color codes:
\033[31mRed\033[32m: green\033[0mReset color
Advanced usage
Output to a file
Use redirection to save output to a file:
Example
echo
"Log entry"
log.txt
> overwrites the file,>> appends content
Command substitution
Output the execution result of another command:
Example
echo
"Current date: $(date)"
Example execution result:
Current date: Tue Jul 25 14:30:22 CST 2023
Formatted output
combined with printf style formatting:
Example
echo
-e
"Name\tAge\n----\t---\nAlice\t25\nBob\t30"
Execution result:
Name Age
---- ---
Alice 25
Bob 30
Practical Application Examples
1. Create a simple menu
Example
#!/bin/bash
echo
"============ Menu ============"
echo
"1. Check system info"
echo
"2. List directory contents"
echo
"3. Show current user"
echo
"4. Exit"
echo
"=============================="
echo
-n
"Please enter your choice [1-4]: "
2. Progress bar simulation
Example
#!/bin/bash
echo
-n
"Progress: ["
for
i
in
{
1
..
20
}
;
do
echo
-n
"#"
sleep
0.1
done
echo
"] Done!"
3. Configuration file generation
Example
#!/bin/bash
config_file
=
"app.conf"
echo
"# Application Configuration"
$config_file
echo
"LOG_LEVEL=DEBUG"
$config_file
echo
"MAX_CONNECTIONS=100"
$config_file
echo
"Configuration file $config_file created"
Common Issues and Notes
-
Quoting issues:
-
Cross-platform differences:
echo may differ across shells#!/bin/bash to specify it explicitly-
Special Character Handling:
-e Be careful with escape characters when using optionsprintf replaces-
Performance considerations:
echo compared to cat more efficientecho to create large files
Linux Command Reference