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

Man Page

echo man page command

回声手册页命令

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

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

echo man page output

Linux echo help output

Linux echo帮助输出

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

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

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

echo command example

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.

如果要显示的文本不带双引号,则输出不会发生变化。 但是,优良作法是使用双引号来强调字符串,如上面的示例所示。

echo command example 2

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 -n option

    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 backspace example

      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

      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. Linux echo tab example

      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. 回声退格示例

      Linux回声退格示例

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

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

      回声换行示例

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

    11. '\ t'水平制表符 :在字符串中出现'\t'任何位置,都会在输出中创建一个制表符空间。
    12. Linux echo选项卡示例

      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

    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文件名

    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 maths example

    Linux echo math operations example

    Linux echo Math操作示例

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

    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命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值