Preface
echo
The 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 --help
not --version
directly 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-e
an anti-escaping function.
# 不加转义符
sean@LAPTOP-BQK5NDTK:~$ echo "abc1\nabc2\nabc3"
abc1\nabc2\nabc3
# 设置转义符
sean@LAPTOP-BQK5NDTK:~$ echo -e "abc1\nabc2\nabc3"
abc1
abc2
abc3
This parameter is very useful. For example, we need to write the following configuration content to a file. Without the -e
newline 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
- 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 mongodb
it, you often need to press 2y
for 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 echo
the 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.