命令行大全

echo

rose1 发表于 2020-09-03 09:06浏览次数:

在类似Unix的操作系统上,echo命令将文本输出到标准输出,例如terminal。 本文档介绍了echo的GNU / Linux版本。

查看英文版

目录:

1 echo 运行系统环境

2 echo 说明

3 echo 语法

4 echo 例子

echo 运行系统环境

Linux

echo 说明

echo是在大多数操作系统中发现的基本命令。它经常在脚本,批处理文件中使用,并作为单个命令的一部分使用。您可能需要输出文本的任何地方。

大多数命令外壳程序(包括bash,kshcsh)都将 echo实现为内置命令。内置的echo命令的行为类似,但是选项可能不同。这些命令未在此处记录。

本文档介绍了独立程序/ bin / echo。它的选项与shell中包含的内置 echo命令略有不同。如果使用的是bash shell,则可以使用type命令确定默认的回显为:

type echo
echo is a shell builtin

要指定您要运行独立程序而不是内置的Shell,请在命令中使用其完整路径,即按以下方式运行它:

/bin/echo

本文档介绍了echo的GNU / Linux独立版本。

echo is a fundamental command found in most operating systems. It is frequently used in scripts, batch files, and as part of individual commands; anywhere you may need to output text.

Most command shells, including bashksh and csh implement echo as a built-in command. The behavior of built-in echo commands is similar, but the options may be different; those commands are not documented here.

This document covers the stand-alone program, /bin/echo. Its options are slightly different than the built-in echo command that is included in your shell. If you are using the bash shell, you can determine which echo is the default, using the type command:

type echo
echo is a shell builtin

To specify that you want to run the stand-alone program instead of the shell built-in, use its complete path in the command, i.e., run it like this:

/bin/echo

This document describes the GNU/Linux stand-alone version of echo.

查看英文版

查看中文版

echo 语法

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

选件

选件

这些选项可以在字符串之前指定,并影响echo的行为。

-n 不要输出尾随换行符。
-e 启用反斜杠转义序列的解释(请参阅下面的列表)。
-E 禁用反斜杠转义序列的解释。这是默认值。

选件

如果指定了long选项,则不能指定要回显的字符串。这些选项仅用于获取有关程序的信息。

--help 显示帮助消息并退出。
--version 输出版本信息并退出。

转义序列

如果指定-e选项,则会在字符串中识别以下转义序列:

序列 解释为
\\ 文字反斜杠字符(“ \ ”)。
\a 警报(BELL字符)。
\b 退格键。
\c 此后不再产生任何输出。
\e 转义字符;等同于按退出键。
\f 一换。
\n 换行符。
\r 一个回车。
\t 水平选项卡。
\v 垂直标签。
\0NNN 具有八进制值NNN的字节(可以为1到3位数字)。
\xHH 具有十六进制值HH的字节(可以为1或2位数字)
注意

每个shell通常都有自己的echo实现,该实现可能与此处描述的版本略有不同。有关其支持的选项的详细信息,请参考您的Shell文档。

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

Options

Options

These options may be specified before the string, and affect the behavior of echo.

-n Do not output a trailing newline.
-e Enable interpretation of backslash escape sequences (see below for a list of these).
-E Disable interpretation of backslash escape sequences. This is the default.

Options

If a long option is specified, you may not specify a string to be echoed. These options are for getting information about the program only.

--help Display a help message and exit.
--version Output version information and exit.

Escape sequences

If you specify the -e option, the following escape sequences are recognized in your string:

Sequence Interpreted as
\\ A literal backslash character ("\").
\a An alert (The BELL character).
\b Backspace.
\c Produce no further output after this.
\e The escape character; equivalent to pressing the escape key.
\f A form feed.
\n A newline.
\r A carriage return.
\t A horizontal tab.
\v A vertical tab.
\0NNN byte with octal value NNN (which can be 1 to 3 digits).
\xHH byte with hexadecimal value HH (which can be either 1 or 2 digits)
NOTE

each shell generally has its own implementation of echo, which may be slightly different than the version described here. Refer to your shell's documentation for details about the options it supports.

查看英文版

查看中文版

echo 例子

/bin/echo Hello, world!

在上面的命令中,两个单词(Hello和world!)作为单独的参数传递给echo,并且echo按顺序打印它们,并用空格隔开:

Hello, world!

下一条命令产生相同的输出:

/bin/echo 'Hello, World!'
Hello, world!

但是,与第一个示例不同,上述命令提供了单引号字符串' Hello,world!。'作为一个参数。

用单引号引起来的字符串将可靠地保护它免受外壳程序的解释,将特殊字符和转义序列直接传递给echo

例如,在bash shell中,变量名前面带有一个美元符号($)。在下一条命令中,引号内的变量名称将按字面意义处理;在引号之外,它将转换为其值。

/bin/echo 'The value of $PATH is' $PATH
The value of $PATH is /home/hope/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

默认情况下,不解释转义序列:

/bin/echo 'Here, \bthe \bbackspace \bsequences \bare \bignored.'
Here, \bthe \bbackspace \bsequences \bare \bignored.

但是,如果提供-e选项,它们将被解释:

/bin/echo -e 'Here, \bhowever, \bthe \bbackspace \bsequences \bare \binterpreted.'
Here,however,thebackspacesequencesareinterpreted.

如果需要在回显输出中插入换行符,请指定-e选项,并在需要换行的位置包括\ n转义序列:

echo -e 'Here,\nwe\nhave\ninserted\nnewlines.'
Here,
we
have
inserted
newlines.

标签也一样:

echo -e 'Here\twe\thave\tinserted\thorizontal\ttabs.'
Here     we      have    inserted        horizontal      tabs.

另一个例子:

echo -e 'This line is not completely \cprinted.'
This line is not completely
/bin/echo Hello, world!

In the above command, the two words (Hello, and world!) are passed to echo as separate arguments, and echo prints them in sequence, separated by a space:

Hello, world!

The next command produces the same output:

/bin/echo 'Hello, World!'
Hello, world!

However, unlike the first example, the above command provides the single-quoted string 'Hello, world!' as a single argument.

Single-quoting a string will reliably protect it from interpretation by the shell, passing special characters and escape sequences literally to echo.

For instance, in the bash shell, variable names are preceded by a dollar sign ($). In the next command, the variable name inside the quotes is treated literally; outside the quotes, it is converted to its value.

/bin/echo 'The value of $PATH is' $PATH
The value of $PATH is /home/hope/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Escape sequences are not interpreted, by default:

/bin/echo 'Here, \bthe \bbackspace \bsequences \bare \bignored.'
Here, \bthe \bbackspace \bsequences \bare \bignored.

However, if you provide the -e option, they will be interpreted:

/bin/echo -e 'Here, \bhowever, \bthe \bbackspace \bsequences \bare \binterpreted.'
Here,however,thebackspacesequencesareinterpreted.

If you need to insert newlines in your echo output, specify the -e option and include the \n escape sequence wherever you want a new line:

echo -e 'Here,\nwe\nhave\ninserted\nnewlines.'
Here,
we
have
inserted
newlines.

Same for tabs:

echo -e 'Here\twe\thave\tinserted\thorizontal\ttabs.'
Here     we      have    inserted        horizontal      tabs.

Another example:

echo -e 'This line is not completely \cprinted.'
This line is not completely

查看英文版

查看中文版