[linux command] echo command and usage tips

Preface

echoThe command is basically a Linux command that we often encounter. I recently used it again at work, and I learned two techniques for using it in the meeting. I will add it to the blog to prevent forgetting.


introduce

We can man命令check which command extensions are supported by echo by using. Of course, if you don't mind the trouble, you can go to some websites to check.

ECHO(1)                                             User Commands                                             ECHO(1)

NAME
       echo - display a line of text

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

DESCRIPTION
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

       -e     enable interpretation of backslash escapes

       -E     disable interpretation of backslash escapes (default)

       --help display this help and exit

       --version
              output version information and exit

       If -e is in effect, the following sequences are recognized:

       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     produce no further output

       \e     escape

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab

       \0NNN  byte with octal value NNN (1 to 3 digits)

       \xHH   byte with hexadecimal value HH (1 to 2 digits)

       NOTE:  your  shell  may  have  its  own  version of echo, which usually supersedes the version described here.
       Please refer to your shell's documentation for details about the options it supports.
  • -n does not output the trailing newline character
  • -e "\a" emits a warning tone
  • -e “\b” deletes the previous character
  • -e "\c" does not add a newline character at the end
  • -e "\f" line break, the cursor stays at the original coordinate position
  • -e "\n" new line, move the cursor to the beginning of the line
  • -e "\r" moves the cursor to the beginning of the line but does not wrap the line
  • -E disables backslash transfer, which is the opposite of the -e parameter.
  • –version View version information
  • –help View help information

PS: The sum of echo is --helpnot --versiondirectly supported. The local test is like this, and it seems that the usage is wrong. Check it later.

# 可以看到, 此处命令直接输出了. 并未进行什么有用的操作.
sean@LAPTOP-BQK5NDTK:~$ echo -E -help
-help
sean@LAPTOP-BQK5NDTK:~$ echo --version
--version

Skill

  • Tip 1: Automatically output newline symbols.
    The reason is that we need to write a newline configuration file in the operation and maintenance script. At this time, we will use -ean anti-escaping function.
# 不加转义符
sean@LAPTOP-BQK5NDTK:~$ echo "abc1\nabc2\nabc3"
abc1\nabc2\nabc3
# 设置转义符
sean@LAPTOP-BQK5NDTK:~$ echo -e "abc1\nabc2\nabc3"
abc1
abc2
abc3

Insert image description here
This parameter is very useful. For example, we need to write the following configuration content to a file. Without the -enewline character set by the parameter, all content will be written in one line. Using escape characters can make our content , placed on different lines. The actual operation is as follows.

For example, we need to write and generate such a configuration file. And name it application.properties.

server=8080
host=127.0.0.1
application=springboot
echo -e "server=8080\nhost=127.0.0.1\napplication=springboot" >> application.properties

Insert image description here


  • Tip 2: Using echo can automatically help us solve problems that sometimes require manual input.

For example, when we use certain commands, it often asks us to enter our password in an invisible place. At this time, it is basically uncomfortable.

  • For example, when using yum install mongodbit, you often need to press 2 yfor yes confirmation.
  • For example, mysql mysql -u root -d. At this time, you will need to enter the password in a hidden environment.
Solution.

Use echothe compound method of adding pipe characters for processing.

echo y y | yum install mongodb

This frees up both hands, and our script can be executed with one click.


Reference

[1]. Linux command encyclopedia - echo command
[2].

Guess you like

Origin blog.csdn.net/u010416101/article/details/121737691