Linux Shell Echo
## 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).
The Shell `echo` command is similar to PHP's `echo` statement, both are used for string output.
**Command Syntax:**
echo
### Why Use echo?
* **Information Feedback**: Display script execution status or results to the user
* **Debugging Tool**: Output variable values or execution locations to help debug scripts
* **Interactive Interface**: Create simple user interaction interfaces
* **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. Output Variables
`echo` 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 mandatory, but it's recommended to use them to avoid surprises:
## Example
echo This is a test
**Execution Result**:
This is a test
* * *
## Common Options
### -n Option: No Newline Output
By default, `echo` adds a newline character after output. Use `-n` to suppress this behavior:
## Example
echo-n"Loading..."
echo" Done!"
**Execution Result**:
Loading... Done!
### -e Option: Enable Escape Character Interpretation
Enable interpretation of backslash escapes:
## Example
echo-e"First linen Second line"
**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
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"33[31mRed Text33[0m"
echo-e"33[42;31mGreen Background with Red Text33[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
* `33[0m` resets all attributes
### 3. Output Command Execution Results
Use command substitution to output command results:
## 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"n33[1mSystem Menu33[0m"
echo"1. Check disk space"
echo"2. List running processes"
echo"3. Show system info"
echo-n"Please enter your choice : "
### 2. Progress Bar Simulation
## Example
echo-n"Progress: ["
for i in{1..20}; do
echo-n"#"
sleep 0.1
done
echo"] Done!"
### 3. Generate Configuration File
## Example
cat<` overwrite file, `>>` append to file |
| Multi-line Output | Use `n` newline or Here Document |
| Portability Advice | Consider using `printf` for complex output |
YouTip