Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use and examples of Linux echo text processing commands

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The description of echo in the linux help documentation is to display a single line of text, similar to print statements in programming languages such as python and java, but it does more than that. You can use man echo to view detailed parameter descriptions.

The echo command is used to output the specified string. The 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 variable name corresponding to the value [root@localhost ~] $echo "hello world" > 1.txt # output string to the specified file [root@localhost ~] $echo `date` # output command execution result

Common parameters:

[root@localhost ~] $echo-n "helloworld" #-n does not output the newline character at the end. By default, it outputs the newline character helloworld [root@localhost ~] $[root@localhost ~] $echo-e "hello\ nworld" #-e to enable backslash escape, for example, it converts to newline helloworld [root@localhost ~] $echo-E "hello\ nworld" #-E to disable backslash escape. Hello\ nworld is disabled by default.

Common escape characters:

[root@localhost ~] $echo-e "hello\\ world" #\\ to output the backslash hello\ world [root@localhost ~] $echo-e "\ a" #\ a for ringing and sounding ringing [root@localhost ~] $echo-e "hello\ bworld" #\ b for backspace Reference: https://blog.csdn.net/lucosax/article/details/34963593hellworld[root@localhost ~] $echo-e "hello\ c world" #\ c after using this escape character, the characters after\ c no longer output hello [root@localhost ~] $echo-e "\ e [32 1m "#\ e is used to control the font and background color helloworld [root@localhost ~] $echo-e" hello\ f hello\ f hello "#\ f line feed, and the cursor stops at the original place after the line feed hello [root@localhost ~] $echo-e" hello\ nworld "#\ n newline character helloworld [root@localhost ~] $echo-e" hello\ rworld "#\ r to move the cursor to the beginning of the line

Echo output Color:

Syntax: echo-e "\ 033 [font background color; font color m string\ 033 [0m"

Example: echo-e "\ 033 [41 countries 36m something here\ 033 [0m"

Explanation: the position of 41 represents the background color of font and the position of 36 represents the color of font.

/ output colored font echo-e "\ 033 [30m black word\ 033 [0m" echo-e "\ 033 [31m red word\ 033 [0m" echo-e "\ 033 [32m green word\ 033 [0m" echo-e "\ 033 [33m yellow word\ 033 [0m" echo-e "\ 033 [34m blue word\ 033 [0m" echo-e "\ 033 [35m purple word\ 033 [0m" echo-e "\ 033] Sky Blue character\ 033 [0m "echo-e"\ 033 [37m White word\ 033 [0m "/ / output font echo-e"\ 033 [40] with background color 37m White character on Black background\ 033 [0m "echo-e"\ 033 [41X 37m White character on Red background\ 033 [0m "echo-e"\ 033] [42X 37m White character on Green background\ 033 [0m "echo-e"\ 033 [43it37m White character on Yellow background\ 033 [0m "echo-e"\ 033 [44trans37m "echo-e"\ 033 [0m "echo-e"\ 033] [45x 37m White character on Purple background\ 033 [0m "echo-e"\ 033] 37m White words on Blue background\ 033 [0m "echo-e"\ 033 [47] 30m black word on white background\ 033 [0m "/ / other attributes\ 33 [0m close all properties\ 33 [1m set high brightness\ 33 [4m underline\ 33 [5m flash\ 33 [7m flicker\ 33 [8m hidden\ 33 [30m -\ 33 [37m set foreground color\ 33 [40m -\ 33 [47m set background color\ 33] [nA cursor up n line\ 33 [nB cursor down n line\ 33 [nC cursor move right n] Line\ 33 [nD cursor move left n line\ 33 [y] XH set cursor position\ 33 [2J clear screen\ 33 [K clear content from cursor to end of line\ 33 [s save cursor location\ 33 [u restore cursor position\ 33 [? 25l hide cursor\ 33 [? 25h display cursor]

Example1: displays a line of text, and no special characters are escaped

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

Example2: displays a line of text, not the newline character at the end of the output

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

Example3: displays a line of text, enabling escape characters after the backslash

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

Example4: displays a line of text, disables the escape characters after the backslash, echo default parameter

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

Example5: the difference between echo and cat. Echo is only used to output text, and cat is used to output file contents or output from standard input.

[root@aliyun-hk1 linux-shell-test] # echo hellohello [root@aliyun-hk1 linux-shell-test] # cat hellocat: 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/hostnamealiyun-hk1 [root@aliyun-hk1 linux-shell-test] # echo hello | cathello [root @ aliyun-hk1 linux-shell-test] #

Examle6: the role of echo in automated builds, for example, we can format the data returned in DB into the data needed by ansible, pass it into a task through with_lines and recycle it. 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.

Ntom 13812345678 HK\ n'| awk'NR > 1 {print $1} '​ TASK [show the items from DB] * Item=robin) = > {"msg": "robin"} ok: [localhost] = > (item=tom) = > {"msg": "tom"}

Example7: echo can also write acquired and formatted data to a file, waiting for subsequent use of ​.

[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.txtrobintom [root@aliyun-hk1 ansible-test] #

So far, this is the end of this article on the use and examples of Linux echo text processing commands. For more information about Linux echo commands, please search for previous articles or continue to browse the relevant articles below. I hope you will support it in the future!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report