echo linux命令
Linux echo command is used to display a line of text on the screen. This is done by echoing the string to the standard output.
Linux echo命令用于在屏幕上显示一行文本。 这是通过将字符串回显到标准输出来完成的。
Linux echo命令 (Linux echo Command)
Let’s see how the echo command is described by the Linux man page. The man page can be accessed by typing the following command:
让我们看看Linux手册页如何描述echo命令。 可以通过键入以下命令来访问手册页:
The following will be displayed as output. Let’s understand the help page part-by-part.
以下将显示为输出。 让我们逐部分地了解帮助页面。
Linux echo命令语法 (Linux echo Command Syntax)
$ echo [OPTION]... [STRING]...
Linux echo命令示例 (Linux echo Command Examples)
There is no change in the output if the text to be displayed is written without double-quotes. However, it’s good practice to use double-quotes to emphasize the string, as shown in the example above.
如果要显示的文本不带双引号,则输出不会发生变化。 但是,优良作法是使用双引号来强调字符串,如上面的示例所示。
Linux echo命令选项 (Linux echo Command Options)
Linux echo Command Options can be used to modify the output of the command as per the user. Let’s go through them one-by-one.
Linux echo命令选项可用于根据用户修改命令的输出。 让我们一一讲解它们。
- -n option can be used to remove the
'\n'
(newline character) from the output. By default, echo includes'\n'
at the end of every string it outputs.
In the example above, since the newline character at the end of the output is omitted, the terminal prompts the user for input in the same line as the output.
-n选项可用于从输出中删除'\n'
(换行符)。 默认情况下,echo在输出的每个字符串的末尾都包含'\n'
。
在上面的示例中,由于省略了输出末尾的换行符,因此终端会提示用户在与输出相同的行中进行输入。
- -e option enables the terminal to recognize escape sequences in the inputted string.
The following sequences are recognized, as described in the Linux man page.
- ‘\\’ backslash character
- ‘\a’ alert (BEL): A sound is played when this character is encountered in an inputted string.
- ‘\b’ backspace: Backspace is triggered when this character is encountered in a string. Hence, the character before
'\b'
is removed from the output, as shown below. - ‘\c’ produce no further output: All characters after
'\c'
in a string are omitted from the output. Note that the terminal prompts for input in the same line as the output. This is because'\n'
– newline character is also removed from the output. - ‘\n’ newline: When encountered in a string, this escape sequence triggers the creation of a new line. As a result, characters after
'\n'
are outputted in a new line. - ‘\r’ carriage return: Characters before
'\r'
in a string are omitted from the output, as shown in the example below. - ‘\t’ horizontal tab: A tab space is created in the output, wherever
'\t'
appears in the string. - ‘\v’ vertical tab: A vertical tab space is created in the output, wherever
'\v'
appears in the string.
如Linux手册页中所述,可以识别以下序列。
- '\\'反斜杠字符
- '\ a'警报(BEL) :在输入的字符串中遇到此字符时会播放声音。
- '\ b'退格键 :在字符串中遇到此字符时将触发退格键。 因此,将从输出中删除
'\b'
之前的字符,如下所示。 - '\ c'不再产生输出 :字符串中
'\c'
之后'\c'
所有字符都将从输出中省略。 请注意,终端在与输出相同的行中提示输入。 这是因为'\n'
n'–换行符也会从输出中删除。 - '\ n'newline :在字符串中遇到时,此转义序列触发创建新行。 结果,
'\n'
之后'\n'
字符将在新行中输出。 - '\ r'回车 :从输出中省略字符串中
'\r'
之前的字符,如下例所示。 - '\ t'水平制表符 :在字符串中出现
'\t'
任何位置,都会在输出中创建一个制表符空间。 - '\ v'垂直制表符 :在字符串中出现
'\v'
任何位置,都会在输出中创建垂直制表符空间。
- -E option when used, disables the recognition of escape sequence in strings. This option is enabled by default. 使用-E选项时,将禁用字符串中转义序列的识别。 默认情况下启用此选项。
- –version and –help display version information and help window respectively and exit afterwards. –version和–help分别显示版本信息和帮助窗口,然后退出。
Linux echo命令高级示例 (Linux echo Command Advanced Examples)
- Linux echo command is used with the wildcard character
'*'
to display the names of all files present in the current directory.
It can also be used in this way to display selected files/folders with certain lowercase/uppercase characters present in their names.
In the example below, only files in the current directory, which have “D” as the initial letter and “s” as the last letter of their name, are displayed as output.
Linux echo命令与通配符'*'
一起使用,以显示当前目录中存在的所有文件的名称。
它也可以这种方式用于显示所选文件/文件夹,其名称中包含某些小写/大写字符。
在下面的示例中,仅将当前目录中名称为首字母“ D”,名称最后一个字母为“ s”的文件显示为输出。
- Linux echo command can be used to perform basic arithmetic operations and display the result as output. This is done by enclosing the mathematical part within
(( ))
preceded by a$
symbol. Linux echo命令可用于执行基本的算术运算并将结果显示为输出。 这是通过将数学部分放在(( ))
内并加$
符号来完成的。 - We can use echo command to display user-defined variables or even environment variables.
我们可以使用echo命令显示用户定义的变量甚至环境变量。
结论 (Conclusion)
Linux echo is a simple, fundamental command that displays a line of text as output. The output can be modified by using the available options along with the command. Among other uses, it can display filenames and values of both user-defined and system variables as well as give the result to some basic arithmetic.
Linux echo是一个简单的基本命令,它将一行文本显示为输出。 可以通过使用可用选项和命令来修改输出。 除其他用途外,它还可以显示用户定义和系统变量的文件名和值,并将结果提供给一些基本的算术运算。
翻译自: https://www.journaldev.com/30389/linux-echo-command-examples
echo linux命令