Echo is popular command comes with all of the Linux distributions. Echo is provided by bash and C shells. Echo simple outputs are given values to the console or terminal.

Echo是所有Linux发行版附带的流行命令。 Echo由bash和C shell提供。 回声简单输出被赋予控制台或终端的值。

(Man Echo)

To get more detailed help and echo official documentation use following command

要获得更详细的帮助并回显官方文档,请使用以下命令

$ man echo



linux echo命令 菜鸟 linux命令echo的用法_python

Man Echo 男人回声

句法(Syntax)

Echo command provides two types of syntax like below.

Echo命令提供两种语法,如下所示。

echo [SHORT-OPTION]... [STRING]...

OR

要么

echo LONG-OPTION

(Display Text)

Simple and most common usage of echo command is just printing specified text to the standard output like below.

echo命令最简单,最常见的用法就是将指定的文本打印到标准输出,如下所示。

$ echo Poftut is very useful



linux echo命令 菜鸟 linux命令echo的用法_python_02

Display Text 显示文字

打印变量(Print Variables)

Another popular usage form is printing shell variables like below.

另一种流行的用法形式是打印如下的shell变量。

$ echo My terminal is $TERM



linux echo命令 菜鸟 linux命令echo的用法_bash_03

Print Variables 打印变量

打印时删除空格(Remove Spaces While Printing)

There is -e option which will remove all spaces by using backspaces defined with \b while printing text

-e选项,它将在打印文本时使用\ b定义的退格键删除所有空格

$ echo -e "This \bis \bpoftut"



linux echo命令 菜鸟 linux命令echo的用法_linux_04

Remove Spaces While Printing 打印时删除空格

重击回声新线(Bash Echo New Line)

Another useful usage is adding \n control character which will print a new line.

另一个有用的用法是添加\ n控制字符,它将打印新行。

$ echo -e "This \nis \npoftut"



linux echo命令 菜鸟 linux命令echo的用法_bash_05

New Line 新队

标签(Tab)

While printing some text to the standard output tabs can be placed into this text by using \t

在将一些文本打印到标准输出选项卡时,可以使用\ t将其放置在此文本中

$ echo -e "This \tis \tpoftut"



linux echo命令 菜鸟 linux命令echo的用法_linux echo命令 菜鸟_06

Tab 标签

垂直标签(Vertical Tab)

Vertical tab will provide new line and align the output text like below.

“垂直”选项卡将提供新行并对齐输出文本,如下所示。

$ echo -e "This \vis \vpoftut"



linux echo命令 菜鸟 linux命令echo的用法_python_07

Vertical Tab 垂直标签

回车(Carriage Return)

A carriage return will delete previous part of the text and only print after last carriage return with \r

回车将删除文本的前半部分,并且仅在最后一次回车后用\ r打印

$ echo -e "This \ris \rpoftut"



linux echo命令 菜鸟 linux命令echo的用法_linux_08

Carriage Return 回车

退格键(Backspace)

Backspace control character will delete a single character to the backward with \b in the text.

退格控制字符将在文本中以\ b向后删除单个字符。

$ echo -e "This \bis \bpoftut"



linux echo命令 菜鸟 linux命令echo的用法_shell_09

Backspace 退格键

省略新行(Omit New Line)

Normally after printing text to the standard output new line is provided to make things more clear. But this new line can be prevented with option -n 

通常,在将文本打印到标准输出之后,会提供新行以使内容更清晰。 但是,可以使用选项-n阻止此新行

$ echo -e -n "This is poftut"



linux echo命令 菜鸟 linux命令echo的用法_python_10

Omit New Line 省略新行

警报(Alert)

Alert can provided to the shell subsystem with \a . This is very useful to make shell scripts and application more interactive

可以使用\ a向外壳子系统提供警报。 这对于使Shell脚本和应用程序更具交互性非常有用

$ echo -e "This is poftut\a"



linux echo命令 菜鸟 linux命令echo的用法_python_11

Alert 警报

This option will not affect the output of the text.

此选项不会影响文本的输出。

(Print All Files and Folders)

Echo command can work with bash features easily. To easily and simply print all files and folders of the current directory following command can be used.

Echo命令可以轻松使用bash功能。 要轻松,简单地打印当前目录的所有文件和文件夹,可以使用以下命令。

$ echo *



linux echo命令 菜鸟 linux命令echo的用法_python_12

Print All Files and Folders 打印所有文件和文件夹

打印特定的扩展名文件(Print specific Extension Files)

While printing files and folders specific extensions can be used to filter output. For example, we want to print to the standard output only c source files with the extension .c 

在打印文件和文件夹时,可以使用特定的扩展名来过滤输出。 例如,我们只想将扩展名为.c的c源文件打印到标准输出中

$ echo *.c



linux echo命令 菜鸟 linux命令echo的用法_linux echo命令 菜鸟_13

Print specific Extension Files 打印特定的扩展名文件

写入文件(Write Into File)

Without opening any text editor some text can be written to a file. The file must not preexist. If file is all ready exist all content is deleted and given text is written to the text file.

无需打开任何文本编辑器,便可以将某些文本写入文件。 该文件不得预先存在。 如果文件已准备就绪,则将删除所有内容,并将给定的文本写入文本文件。

$ echo "This is poftut" > mystory.txt



linux echo命令 菜鸟 linux命令echo的用法_bash_14

Write Into File 写入文件

创建空文件(Create Empty File)

An empty file can be created by providing an empty string definition and redirecting this to a file.

可以通过提供空字符串定义并将其重定向到文件来创建空文件。

$ echo "" > empty_file.txt



linux echo命令 菜鸟 linux命令echo的用法_python_15

Create Empty File 创建空文件

添加到文件(Add To The File)

The last example of the echo command is adding more text to the existing file. Adding operation will no delete existing text in the file.

echo命令的最后一个示例是将更多文本添加到现有文件。 添加操作不会删除文件中的现有文本。

$ echo "This is poftut" >> mystory.txt



linux echo命令 菜鸟 linux命令echo的用法_linux echo命令 菜鸟_16

Add To The File 添加到文件

翻译自: https://www.poftut.com/linux-echo-command-usage-examples/