echo linux命令_Linux echo命令示例

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命令。 可以通过键入以下命令来访问手册页:

echo linux命令_Linux echo命令示例_第1张图片

echo man page command

回声手册页命令

The following will be displayed as output. Let’s understand the help page part-by-part.

以下将显示为输出。 让我们逐部分地了解帮助页面。

echo linux命令_Linux echo命令示例_第2张图片

Linux echo help output

Linux echo帮助输出

Linux echo命令语法 (Linux echo Command Syntax)

$ echo [OPTION]... [STRING]...

Linux echo命令示例 (Linux echo Command Examples)

echo linux命令_Linux echo命令示例_第3张图片

echo command output

回显命令输出

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 Command Example

Linux echo命令示例

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.
    echo linux命令_Linux echo命令示例_第4张图片

    Linux echo -n option example

    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'

    Linux 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.

    1. ‘\\’ backslash character
    2. ‘\a’ alert (BEL): A sound is played when this character is encountered in an inputted string.
    3. ‘\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.
    4. echo linux命令_Linux echo命令示例_第5张图片

      Linux echo backspace example

    5. ‘\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.
    6. Linux echo stop output example

    7. ‘\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.
    8. echo newline example

    9. ‘\r’ carriage return: Characters before '\r' in a string are omitted from the output, as shown in the example below.
    10. Linux echo carriage return example

    11. ‘\t’ horizontal tab: A tab space is created in the output, wherever '\t' appears in the string.
    12. echo linux命令_Linux echo命令示例_第6张图片

      Linux echo tab example

    13. ‘\v’ vertical tab: A vertical tab space is created in the output, wherever '\v' appears in the string.
    14. Linux echo vertical tab example

    -e选项使终端能够识别输入字符串中的转义序列。

    如Linux手册页中所述,可以识别以下序列。

    1. '\\'反斜杠字符
    2. '\ a'警报(BEL) :在输入的字符串中遇到此字符时会播放声音。
    3. '\ b'退格键 :在字符串中遇到此字符时将触发退格键。 因此,将从输出中删除'\b'之前的字符,如下所示。
    4. echo linux命令_Linux echo命令示例_第7张图片

      Linux回声退格示例

    5. '\ c'不再产生输出 :字符串中'\c'之后'\c'所有字符都将从输出中省略。 请注意,终端在与输出相同的行中提示输入。 这是因为'\n' n'–换行符也会从输出中删除。
    6. Linux回声停止输出示例

    7. '\ n'newline :在字符串中遇到时,此转义序列触发创建新行。 结果, '\n'之后'\n'字符将在新行中输出。
    8. 回声换行示例

    9. '\ r'回车 :从输出中省略字符串中'\r'之前的字符,如下例所示。
    10. Linux回声回车示例

    11. '\ t'水平制表符 :在字符串中出现'\t'任何位置,都会在输出中创建一个制表符空间。
    12. echo linux命令_Linux echo命令示例_第8张图片

      Linux echo选项卡示例

    13. '\ v'垂直制表符 :在字符串中出现'\v'任何位置,都会在输出中创建垂直制表符空间。
    14. Linux echo垂直制表符示例

  • -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)

  1. Linux echo command is used with the wildcard character '*' to display the names of all files present in the current directory.

    Linux echo filenames example

    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 selected filenames example

    Linux echo命令与通配符'*'一起使用,以显示当前目录中存在的所有文件的名称。

    Linux echo文件名示例

    它也可以这种方式用于显示所选文件/文件夹,其名称中包含某些小写/大写字符。

    在下面的示例中,仅将当前目录中名称为首字母“ D”,名称最后一个字母为“ s”的文件显示为输出。

    Linux echo选择的文件名示例

  2. 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命令可用于执行基本的算术运算并将结果显示为输出。 这是通过将数学部分放在(( ))内并加$符号来完成的。
  3. echo linux命令_Linux echo命令示例_第9张图片

    Linux echo math operations example

    Linux echo Math操作示例

  4. We can use echo command to display user-defined variables or even environment variables.

    echo variable value display example

    我们可以使用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命令

你可能感兴趣的:(字符串,linux,python,java,shell)