Shell - echo, printf and color printing

printf

printf format

printf "format string" variable

The use of printf is the same as in C language, and does not require parentheses and commas to separate:

printf "%-10s %-5d\n" hello_world 

Print result:

hello_world

When printing a string, if there are spaces or special symbols in the string, it needs to be enclosed in double quotes:

printf "%-10s %-5d\n" "hello world"

Print result:

hello world

Common escape characters

escape character illustrate
\n line break
\t horizontal tab
\v vertical tab
\a warning character
\b backspace
\f Feed character
\r carriage return
? Used when writing multiple question marks in succession
used to represent the character '
" Used to represent the character "
\\ Used to represent the character \
\ddd Octal number printing, where ddd means printing a 1~3 octal number
\xdd Hexadecimal number printing, where dd means 1~2 hexadecimal numbers

echo

Unlike printf, echo will automatically wrap each time it prints

echo print format

echo [Options can be added] [Print content]

options illustrate
-n Disable word wrap
-e Allow printing with escape characters
-E Disallow \escaping characters with

1) -n option

[wjj@learning study]$ echo -n Hello World!
Hello World![wjj@learning study]$ 

Because the automatic line feed function is canceled, the printed result is on the same line as the next command.

2) -e option

[wjj@learning study]$ echo -e "zhangsan\nlisi\nwangwu"
zhangsan
lisi
wangwu

The -e option allows echo to print using the above escape symbols.

3) -E option

[wjj@learning study]$ echo -E "zhangsan\nlisi\nwangwu"
zhangsan\nlisi\nwangwu

The -E option cancels \the escape function.


Three ways of echo printing

1) Print without quotes

Format:echo 字符串

echo hello world 

2) Print the contents of the double quotes

Format:echo "字符串"

echo "hello world"

3) Print the content in single quotes

Formatecho ‘字符串’

echo 'hello world'

The difference between the three printing methods

  • The unquoted way cannot be printed ;because ;in the shell as a command delimiter
  • The way of double quotes supports parsing special characters. If you don’t want to parse special characters, you need to add escape characters\
  • The way of single quotes does not support all special symbols, what you see is what you get

Escape characters supported by echo

Most of the escape characters supported by echo are the same as those of the printf function, and some of them are different, which need to be compared with the previous table.

escape character illustrate Remarks (whether it is the same as the printf function)
\a warning character
\b backspace
\c Suppress printing characters (do not print characters after \c) ×
\e Escape characters (see color printing below for examples) ×
\f Near paper character (newline end alignment)
\n line break
\r carriage return
\t horizontal tab
\v vertical tab
\\ character'\'
\0ddd Octal number printing, where ddd means printing a 1~3 octal number (slightly different from printf) ×
\xdd Hexadecimal number printing, where dd means 1~2 hexadecimal numbers

About printing exclamation point (!)

As a special symbol in the shell !, you need to pay attention to the following issues when printing it

1) printf and echo pair !escape printing

[wjj@learning ~]$ printf "Hello World\!\n"
Hello World\!
[wjj@learning ~]$ echo "Hello World\!"
Hello World\!

After !escaping and printing, the printing will not report an error, but the printing will appear \. This situation will only appear when the command is entered on the command line. Using the script to print can !normally escape.

2) printf formatted output

[wjj@learning ~]$ printf "%-s %-s %c\n" Hello World !
Hello World !

By formatting the output, !print it as a variable, and the print result is correct.

3) echo prints without quotes

[wjj@learning ~]$ echo Hello World !
Hello World !

echo can correctly output the result without quotes, but this method is not conducive to scripting, so it is not recommended.

4) ‘’print with echo

[wjj@learning ~]$ echo 'Hello World!'
Hello World!

Because the single quotation mark method does not support all special symbols, what you see is what you get, so it can be printed normally, and this method is recommended


color output

General format:echo -e "\e[am 文本 \e[am"

Where a is the corresponding color ASCII code value, used to control 文本颜色, 背景颜色and 文本样式.

文本颜色mainly include:

reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, white=37

背景颜色mainly include:

reset=0, black=40, red=41, green=42, yellow=43, blue=44, magenta=45, cyan=46, white=47

文本样式mainly include:

reset 0, set highlight=1, underline=4, blink=5, reverse=7, blank=8,

Example 1: print red font:

echo -e "\e[31m red text \e[0m"

in:

  • "\e[31m \e[0m" is the standard format
  • \e[31m is an escape character, 31 corresponds to the red color of the text
  • \e[0m is to reset the text, if not reset, it will keep the format

The effect is as follows:

insert image description here

Example 2: print red font, yellow background

echo -e "\e[43;31m red text \e[0m"

in:

  • 43;31m corresponds to the background color yellow and the font color black respectively, the order of the two is not fixed, and the final system operates according to the value corresponding to the ASCII code.

The effect is as follows:
insert image description here

Example 3: print red font, yellow background, underlined

echo -e "\e[4;43;31m red text \e[0m"
  • By continuously adding control options, the text display is more beautiful

The effect is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/why1472587/article/details/128503306