TrumanWong

echo

Output the specified string or variable

Supplementary instructions

echo command is used to print the value of shell variables in the shell, or directly output the specified string. The Linux echo command is very commonly used in shell programming. It is also often used when printing the value of a variable in the terminal. Therefore, it is necessary to understand the usage of echo. The function of the echo command is to display a piece of text on the monitor. It generally serves as a The role of prompts.

grammar

echo(option)(parameter)

Options

-e: Enable escape characters.
-E: disable escape characters (default)
-n: No newline at the end

When using the -e option, if the following characters appear in the string, they will be specially processed and will not be output as ordinary text:

  • \a emits a warning sound;
  • \b deletes the previous character;
  • \c produces no further output (characters after \c will not be output);
  • \f wraps a new line but the cursor remains at the original position;
  • \n wraps the line and moves the cursor to the beginning of the line;
  • \r moves the cursor to the beginning of the line, but does not wrap the line;
  • \t insert tab;
  • \v is the same as \f;
  • \\ inserts \ character;
  • \nnn inserts the ASCII character represented by nnn (octal);

Parameters

Variable: Specify the variable to print.

Example

/bin/echo Hello, world!

In the above command, two words (Hello and world!) are passed to echo as separate arguments, and echo prints them in order, separated by spaces

The next command produces the same output:

/bin/echo 'Hello, World!'

However, unlike the first example, the above command provides the single-quoted string 'Hello, world!' as a single argument.

Single quotes will reliably protect it from shell interpretation, passing special characters and escape sequences to echo verbatim.

For example, in the bash shell, variable names are preceded by a dollar sign ($). In the next command, the variable name within quotes is treated literally; outside quotes, it is converted to its value.

/bin/echo 'The value of $PATH is' $PATH
# The value of $PATH is
# /home/hope/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Use the echo command to print colored text:

Text color:

echo -e "\e[1;31mThis is red text\e[0m"
This is red text
  • \e[1;31m Set color to red
  • \e[0m reset the color back

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

echo -e "\x1b[30;1m 0 black \x1b[0m"\
"\x1b[31;1m 1 red \x1b[0m"\
"\x1b[32;1m 2 green \x1b[0m"\
"\x1b[33;1m 3 yellow \x1b[0m"\
"\x1b[34;1m 4 blue \x1b[0m"\
"\x1b[35;1m 5 magenta \x1b[0m"\
"\x1b[36;1m 6 cyan \x1b[0m"\
"\x1b[37;1m 7 white \x1b[0m"

Background Color:

echo -e "\e[1;42mGreed Background\e[0m"
Greed Background

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

Text flashing:

echo -e "\033[37;31;5mMySQL Server Stop...\033[39;49;0m"

There are other digital parameters at the red numbers: 0 turns off all attributes, 1 sets high brightness (bold), 4 underline, 5 flashes, 7 inverts, 8 blanks

No newline character is added at the end of the output

echo -n 'hello'