Linux echo text processing command usage and examples

Linux echo text processing command usage and examples

The description of echo in the Linux help document is to display a line of text, similar to the print statement in programming languages ​​​​such as Python and Java, but in fact it does more than that. You can use man echo to view detailed parameter descriptions.

The echo command is used to output a specified string. Common usage is as follows:

[root@localhost ~]$ echo # Output a blank line[root@localhost ~]$ echo "hello world" # Output the specified string[root@localhost ~]$ echo $HOSTNAME # Output the value corresponding to the variable name[root@localhost ~]$ echo "hello world" > 1.txt # Output the string to the specified file[root@localhost ~]$ echo `date` # Output the execution result of the command

Common parameters:

[root@localhost ~]$ echo -n "hello world" # -n does not output a newline at the end. By default, a newline will be output at the end. hello world[root@localhost ~]$

[root@localhost ~]$ echo -e "hello\nworld" # -e is used to enable backslash escape, such as \n will be converted to newline hello
world

[root@localhost ~]$ echo -E "hello\nworld" # -E is used to disable backslash escape, which is disabled by default.

Common escape characters:

[root@localhost ~]$ echo -e "hello \\ world" # \\ is used to output backslash hello \ world

[root@localhost ~]$ echo -e "\a" # \a is used for ringing, a ringing sound[root@localhost ~]$ echo -e "hello\bworld" # \b is used for backspace, reference: https://blog.csdn.net/lucosax/article/details/34963593
hellworld

[root@localhost ~]$ echo -e "hello \c world" # After using this escape character, the characters after \c will no longer output hello 

[root@localhost ~]$ echo -e "\e[32;1m hello world \e[35;1m" # \e is used to control the font and background color hello world 

[root@localhost ~]$ echo -e "hello \f hello \f hello" # \f new line, and the cursor stops at the original place after the new line hello 
    hello
       hello

[root@localhost ~]$ echo -e "hello\nworld" # \n newline character hello
world

[root@localhost ~]$ echo -e "hello\rworld" # \r is used to move the cursor to the beginning of the line, which is equivalent to deleting the characters before \r and only outputting the characters after \r world

[root@localhost ~]$ echo -e "hello\tworld" # \t Tab character, equivalent to the Tab key on the keyboard hello world

[root@localhost ~]$ echo -e "hello\vworld" # \v vertical tab hello
   world

echo output color:

Syntax: echo -e "\033[font background color; font color m string\033[0m"
Example: echo -e "\033[41;36m something here \033[0m"
Explanation: The 41 position represents the font background color, and the 36 position represents the font color.

// Output colored font echo -e "\033[30m black font\033[0m"
echo -e "\033[31m red word\033[0m"
echo -e "\033[32m green word\033[0m"
echo -e "\033[33m yellow text\033[0m"
echo -e "\033[34m blue words\033[0m"
echo -e "\033[35m purple text\033[0m"
echo -e "\033[36m sky blue word\033[0m"
echo -e "\033[37m white word\033[0m"
// Output font with background color echo -e "\033[40;37m black background white text\033[0m"
echo -e "\033[41;37m Red background with white characters\033[0m"
echo -e "\033[42;37m green background with white text\033[0m"
echo -e "\033[43;37m yellow background white text\033[0m"
echo -e "\033[44;37m blue background white text\033[0m"
echo -e "\033[45;37m purple background white text\033[0m"
echo -e "\033[46;37m sky blue background with white text\033[0m"
echo -e "\033[47;30m Black text on white background\033[0m"
//Other properties\33[0m Turn off all properties\33[1m Set high brightness\33[4m Underline\33[5m Blink\33[7m Reverse display\33[8m Hide\33[30m — \33[37m Set foreground color\33[40m — \33[47m Set background color\33[nA Move cursor up n lines\33[nB Move cursor down n lines\33[nC Move cursor right n lines\33[nD Move cursor left n lines\33[y;xH Set cursor position\33[2J Clear screen\33[K Clear the content from cursor to the end of the line\33[s Save cursor position\33[u Restore cursor position\33[?25l Hide cursor\33[?25h Show cursor

Example 1: Display a line of text, any special characters will not be escaped

[root@aliyun-hk1 linux-shell-test]# echo hello\nworld
hellonworld
[root@aliyun-hk1 linux-shell-test]# echo 'hello\nworld'
hello\nworld
[root@aliyun-hk1 linux-shell-test]# echo hello world
hello world
[root@aliyun-hk1 linux-shell-test]#

Example 2: Display a line of text without outputting the trailing newline character

[root@aliyun-hk1 linux-shell-test]# echo -n hello world
hello world[root@aliyun-hk1 linux-shell-test]# echo hello world
hello world

example3: Display a line of text, enabling escape characters after backslash

[root@aliyun-hk1 linux-shell-test]# echo -e 'hello\nworld'
hello
world
[root@aliyun-hk1 linux-shell-test]# echo -e 'hello\tworld'
hello world

Example 4: Display a line of text, disable escape characters after backslash, echo default parameters

[root@aliyun-hk1 linux-shell-test]# echo -E 'hello\nworld'
hello\nworld
[root@aliyun-hk1 linux-shell-test]# echo -E 'hello\tworld'
hello_tworld

Example 5: Comparison of the differences between echo and cat. echo is only used to output text, while cat is used to output file contents or output from standard input.

[root@aliyun-hk1 linux-shell-test]# echo hello
hello
[root@aliyun-hk1 linux-shell-test]# cat hello
cat: hello: No such file or directory
[root@aliyun-hk1 linux-shell-test]# echo /etc/hostname
/etc/hostname
[root@aliyun-hk1 linux-shell-test]# cat /etc/hostname
aliyun-hk1
[root@aliyun-hk1 linux-shell-test]# echo hello|cat
hello
[root@aliyun-hk1 linux-shell-test]#

examle6: The role of echo in automated construction. For example, we can format the data returned in the DB into the data required by Ansible, pass it to a task through with_lines and use it in a loop. In some cases, the standard output obtained from the network, DB, etc. can be formatted or cleaned by echo combined with awk and grep, and then used in subsequent scripts.

[root@aliyun-hk1 linux-shell-test]# echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'
name phone addr
robin 13712345678 CN
tom 13812345678 HK
[root@aliyun-hk1 linux-shell-test]# echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'|awk 'NR>1 {print $1}'
robin
tom
- name: show the items from DB
   debug:
    msg: "{{ item }}"
   with_lines: "echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'|awk 'NR>1 {print $1}'
​
TASK [show the items from DB] ******************************************************************************************************************************************************************************************************************************** ok: [localhost] => (item=robin) => {
  "msg": "robin"
}
ok: [localhost] => (item=tom) => {
  "msg": "tom"
}

example7: echo can also write the acquired and formatted data to a file for subsequent use.

[root@aliyun-hk1 ansible-test]# echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'|awk 'NR>1 {print $1}' > DataFromDB1.txt
[root@aliyun-hk1 ansible-test]# cat DataFromDB1.txt
robin
tom
[root@aliyun-hk1 ansible-test]#

This is the end of this article about the usage and examples of the Linux echo text processing command. For more related Linux echo command content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use of Linux echo command and three implementation methods
  • Linux echo command and solution to the problem of insufficient permissions for the Linux echo command
  • How to use echo command in Linux

<<:  Summary of methods for cleaning Mysql general_log

>>:  A super detailed Vue-Router step-by-step tutorial

Recommend

Detailed explanation of how to use the vue verification code component

This article example shares the specific implemen...

Detailed explanation of Nginx http resource request limit (three methods)

Prerequisite: nginx needs to have the ngx_http_li...

Solution to the problem of repeated pop-up of Element's Message pop-up window

Table of contents 1. Use 2. Solve the problem of ...

Comparing Node.js and Deno

Table of contents Preface What is Deno? Compariso...

How to configure two-way certificate verification on nginx proxy server

Generate a certificate chain Use the script to ge...

CSS injection knowledge summary

Modern browsers no longer allow JavaScript to be ...

Detailed process of using nginx to build a webdav file server in Ubuntu

Install nginx Note that you must install nginx-fu...

Solution to MySQL startup successfully but not listening to the port

Problem Description MySQL is started successfully...

How to deploy and start redis in docker

Deploy redis in docker First install Docker in Li...

How to create, save, and load Docker images

There are three ways to create an image: creating...

HTML page common style (recommended)

As shown below: XML/HTML CodeCopy content to clip...

How to distinguish MySQL's innodb_flush_log_at_trx_commit and sync_binlog

The two parameters innodb_flush_log_at_trx_commit...

How to use video.js in vue to play m3u8 format videos

Table of contents 1. Installation 2. Introducing ...