Linux command learning: echo

  The echo command is used to print the value of the shell variable in the shell, or directly output the specified string. The echo command of linux is very commonly used in shell programming. It is also often used when printing variable values ​​under 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 display, which generally serves as a The role of prompting.

grammar

echo (options) (arguments)

Options

-e: activate escape characters.

  When using the -e option, if the following characters appear in the string, they will be treated specially instead of being output as normal text:

\a sounds a warning;
\b deletes the previous character;
\c does not add a newline at the end;
\f wraps the 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 line First, but not newline;
\t inserts tab;
\v is the same as \f;
\\ inserts \ character;
\nnn inserts the ASCII character represented by nnn (octal);

parameter

Variable: Specify the variable to print.

example

  1. Enter a line of text and display it on standard output

$ echo Tecmint is a community of Linux Nerds 
# will output the following text:

Tecmint is a community of Linux Nerds

  2. Output a declared variable value

# For example, declare the variable x and assign it the value 10.
$ x=10
# will output its value:
$ echo The value of variable x = $x 

The value of variable x = 10 

  3. Use the '\b' option

# '-e' followed by ' \b ' will remove all spaces between characters.
$ echo -e "Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds" 

TecmintisacommunityofLinuxNerds

  Note: The option '-e' in Linux acts as a translator for the escape character backslash.

4. Use the '\n' option

# The line with '\n' after '-e' will be used as a new line where it is encountered
$ echo -e " Tecmint \ nis \ na \ ncommunity \ nof \ nLinux \ nNerds " 

Tecmint
is
a
community
of
Linux
Nerds

5. Use the '\t' option

# ' -e' followed by '\t' adds a horizontal tab between spaces.
$ echo -e " Tecmint \ tis \ ta \ tcommunity \ tof \ tLinux \ tNerds " 

Tecmint is a community of Linux Nerds

6. You can also use newline '\n' and horizontal tab '\t' at the same time

$ echo -e "\n\tTecmint \n\tis \n\ta \n\tcommunity \n\tof \n\tLinux \n\tNerds" 

Tecmint
is
a
community
of
Linux
Nerds

7. Use the '\v' option

$ echo -e "\vTecmint \vis \va \vcommunity \vof \vLinux \vNerds" 
 
Tecmint
        is
           a
             community
                       of
                          Linux
                                Nerds

8. You can also use newline '\n' and vertical tab '\v' at the same time

$ echo -e "\n\vTecmint \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds" 
 
 
Tecmint
 
is
 
a
 
community
 
of
 
Linux
 
Nerds

  Note: You can use two or more vertical tabs, horizontal tabs and newlines in succession according to your needs.

9. Use the '\r' option

# ' -e' followed by '\r' to specify a carriage return in the output. (LCTT Annotation: It will overwrite the characters at the beginning of the line)
$ echo -e "Tecmint \ris a community of Linux Nerds" 

is a community of Linux Nerds

10. Use the '\c' option

# ' -e' followed by '\c' suppresses the output of the following characters and does not end up with a newline.
$ echo -e "Tecmint is a community \cof Linux Nerds" 

Tecmint is a community @tecmint:~$ 

11. '-n' will not output a new line after echo

$ echo -n "Tecmint is a community of Linux Nerds" 
Tecmint is a community of Linux Nerds@tecmint:~/Documents$ 

12. Using the '\a' option

# ' -e' followed by the '\a' option to hear an audible warning.
$ echo -e "Tecmint is a community of \aLinux Nerds" 

Tecmint is a community of Linux Nerds

  Note: Before you start, please check your volume settings.

13. Use echo command to print all files and folders (alternative to ls command)

$ echo * 

103.odt 103.pdf 104.odt 104.pdf 105.odt 105.pdf 106.odt 106.pdf 107.odt 107.pdf 108a.odt 108.odt 108.pdf 109.odt 109.pdf 110b.odt 110.odt 
110.pdf 111.odt 111.pdf 112.odt 112.pdf 113.odt linux-headers-3.16.0-customkernel_1_amd64.deb linux-image-3.16.0-customkernel_1_amd64.deb
network.jpeg

14. Print the specified file type

# For example, let's say you want to print all '.jpeg' files, use the following command.
$ echo * .jpeg

network.jpeg

15. echo can use redirection to output to a file instead of stdout

$ echo "Test Page" > testpage 

## Check Content
avi@tecmint:~$ cat testpage 
Test Page

16. Display escape characters

echo "\"It is a test\""
# The result will be:
" It is a test " 
#Similarly, double quotes can also be omitted

17. Output the string as is, without escaping or taking variables (with single quotes)

echo '$name\"'
# output result:
$name\"

18. Display command execution result

echo `date`
#The result will show the current date

Thu Jul 24  10:08:46 CST 2014 _ _ _

19. Use the echo command to print text with color:

echo -e "\e[1;31mThis is red text \e[0mThis is another text"
This is red text This is another text
# \e[ 1 ;31m set the 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

19. Use the echo command to print text with a 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

19. Use the echo command to print the flashing text:

echo -e "\033[37;31;35mMySQL Server Stop...\033[39;49;0m  need to restart "
MySQL Server Stop...  need to restart   

  There are other digital parameters at the red numbers: 0 to turn off all attributes, 1 to set highlight (bold), 4 to underline, 5 to flash, 7 to invert, 8 to blank

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889237&siteId=291194637