Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux echo command

Linux echo command

Linux Command Reference Linux Command Reference

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:

Basic Syntax

echo [选项] [字符串]

**Common Options: **

Escape characters (require the -e option):


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:

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:


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

  1. Quoting issues:

  2. Double quotes: variables and special characters are interpreted
  3. Single quotes: everything is output literally
  4. No quotes: multiple spaces are collapsed into one
  5. - Double quotes: variables and special characters are interpreted - Single quotes: everything is output literally - No quotes: multiple spaces are collapsed into one
    1. Cross-platform differences:

    2. The behavior of echo may differ across shells
    3. For scripts, it is recommended to use #!/bin/bash to specify it explicitly
    4. - The behavior of `echo` may differ across shells - For scripts, it is recommended to use `#!/bin/bash` to specify it explicitly
      1. Special Character Handling:

      2. Use -e Be careful with escape characters when using options
      3. when unsure, you can use printf replaces
      4. - Be careful with escape characters when using the `-e` option - When unsure, you can use `printf` instead
        1. Performance considerations:

        2. When producing large amounts of output,echo compared to cat more efficient
        3. avoid using echo in loops echo to create large files
        4. - When producing large amounts of output, `echo` is more efficient than `cat` - Avoid using `echo` in loops to create large files

          Linux Command Reference Linux Command Reference