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

echo "Hello, World!"

Execution result:

Hello, World!

2. Output variables

echoYou 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 it is recommended to use them to avoid accidents:

Example

echo This is a test

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 -n "Loading..."
echo " Done!"

Execution result:

Loading... Done!

-e option: enable escape character interpretation

Enable interpretation of backslash escapes:

Example

echo -e "First line\nSecond 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 to implement 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 above colors
  • \033[0mReset all attributes

3. Output command execution result

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 "\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 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 have problems if the variable contains spaces
echo "$var"  # Correct way

2. Implementation differences between different shells:

  • Bash built-inechoSupports-eoption
  • On some systems,/bin/echomay not support all options
  • For portable scripts, it is recommended to useprintfto replace complexecho

3. Special character handling:

Example

echo "Cost: \$100"  # Output $ symbol
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
Other extensions