echo: Printing Strings or Variables in Terminal

Overview
The echo command is used to display strings or variables in the terminal.

Usage

echo [options] [args]

Description
echo is ideal for outputting text, displaying variables, and creating formatted outputs.

Options

  • -e: Enables escape characters such as newline \n and tab \t. Common escape sequences include:
    • \n: Newline
    • \t: Horizontal tab
    • \\: Backslash
    • \": Double quote
    • \a: Alert (beep)
    • \b: Backspace
    • \v: Vertical tab
  • -n: Prevents auto newline at the end of the output. By default, echo will automatically add a newline, which -n can disable.

Arguments

  • args: One or more strings or variables to be printed.

Examples

1 Simple Text Output

$ echo "Hello, World!"

Output:
Hello, World!

2 Display Variable Values
You can print variable values by prefixing the variable name with $:

$ name="Alice"
$ echo "Hello, $name"

Output:
Hello, Alice

To display environment variables:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

$ echo $HOME
/home/user

$ echo $USER
user

3 Using Command Substitution
echo can use command substitution with $(command) to insert the output of a command:

$ echo "Today is $(date)"

Output:
Today is Sun Oct 25 10:20:22 AM

4 Multi-line Output
Use the -e option to enable escape characters like \n for multi-line output:

$ echo -e "Line 1\nLine 2\nLine 3"

Output:

Line 1
Line 2
Line 3

5 Suppress Auto Newline
To suppress the newline, use -n:

$ echo -n "Hello, World!"

Output:
Hello, World! (without newline)

6 Using Escape Characters
Enable escape characters with -e for features like tabs (\t) and newlines (\n):

$ echo -e "Column1\tColumn2\nData1\tData2"

Output:

Column1    Column2
Data1      Data2

7 Redirect Output to File
Redirect echo output to a file. Use > to overwrite or >> to append:

$ echo "Hello, World!" > output.txt    # Overwrite
$ echo "Another Line" >> output.txt     # Append

8 Output Strings with Special Characters
For special characters (like $, “, ), use single quotes or escape them with \:

$ echo "This is a dollar sign: \$ and a quote: \""

Output:
This is a dollar sign: $ and a quote: “

echo用于在终端打印字符串或变量的值

功能说明:echo用于在终端打印字符串或变量的值

语  法:echo [options] [args]

补充说明:echo非常适合输出文本信息、打印变量、生成格式化输出等

   项:

-e           启用转义字符,如换行 \n、制表符 \t 等。常用转义字符如下:

\n    换行

\t     水平制表符(Tab)

\\     反斜杠

\”    双引号

\a    警报(蜂鸣)

\b    退格

\v    垂直制表符

-n           禁止自动换行。默认情况下,echo 命令会在输出末尾自动添加换行符,使用 -n 可以取消自动换行

   数:

args       要输出的一个或多个字符串或变量

   例:

1 简单输出文本

$ echo “Hello, World!”

Hello, World!

2 输出变量(包括环境变量)的值

可以使用 echo 打印变量的值,在变量名前加 $ 符号:

$ name=”Alice”

$ echo “Hello, $name”

Hello, Alice

输出环境变量的值:

$ echo $PATH

/home/john/.config/composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

$ echo $HOME

/home/john

$ echo $USER

john

3 使用变量或命令替换

echo 可以和命令替换 $(command) 一起使用,将命令的输出结果插入到输出中:

$ echo “Today is $(date)”

Today is Sun Oct 25 10:20:22 AM

4 输出多行文本

使用 -e 参数可以使 \n 等转义字符生效,从而输出多行文本:

$ echo -e “Line 1\nLine 2\nLine 3”

Line 1

Line 2

Line 3

5 禁止自动换行

echo 默认在输出末尾添加换行符,可以使用 -n 禁止换行:

$ echo -n “Hello, World!”

# 输出:Hello, World!(无换行)

6使用转义字符

使用 -e 参数可以启用多种转义字符,如 \t (制表符) 和 \n (换行符) 等:

$ echo -e “Column1\tColumn2\nData1\tData2”

Column1   Column2

Data1     Data2

7 重定向输出到文件

可以将 echo 的输出重定向到文件中,> 会覆盖文件内容,而 >> 会追加内容:

$ echo “Hello, World!” > output.txt    # 覆盖写入到文件

$ echo “Another Line” >> output.txt     # 追加写入到文件

8 输出包含特殊字符的字符串

如果字符串中包含特殊字符(如 $, “, \),可以用单引号或在特殊字符前加 \ 进行转义:

$ echo “This is a dollar sign: \$ and a quote: \” “

This is a dollar sign: $ and a quote: “