mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
[Translated] 15 Practical Examples of ‘echo’ command in Linux
This commit is contained in:
parent
cf9353fbd6
commit
02f522100e
@ -1,44 +1,41 @@
|
||||
Translating-----------geekpi
|
||||
|
||||
|
||||
15 Practical Examples of ‘echo’ command in Linux
|
||||
Linux中15个‘echo’ 实例
|
||||
================================================================================
|
||||
**echo** is one of the most commonly and widely used built-in command for Linux bash and C shells, that typically used in scripting language and batch files to display a line of text/string on standard output or a file.
|
||||
**echo**是一种最常用的与广泛使用的内置于Linux的bash和C shell的命令,通常用在脚本语言和批处理文件中来在标准输出或者文件中显示一行文本或者字符串。
|
||||
|
||||
![echo command examples](http://www.tecmint.com/wp-content/uploads/2014/08/echo-command.png)
|
||||
echo command examples
|
||||
echo命令例子
|
||||
|
||||
The syntax for echo is:
|
||||
echo命令的语法是:
|
||||
|
||||
echo [option(s)] [string(s)]
|
||||
echo [选项] [字符串]
|
||||
|
||||
**1.** Input a line of text and display on standard output
|
||||
**1.** 输入一行文本并显示在标准输出上
|
||||
|
||||
$ echo Tecmint is a community of Linux Nerds
|
||||
|
||||
Outputs the following text:
|
||||
会输出下面的文本:
|
||||
|
||||
Tecmint is a community of Linux Nerds
|
||||
|
||||
**2.** Declare a variable and echo its value. For example, Declare a variable of **x** and assign its value=**10**.
|
||||
**2.** 声明一个变量并输出它的值。比如,声明变量**x**并给它赋值为**10**。
|
||||
|
||||
$ x=10
|
||||
|
||||
echo its value:
|
||||
会输出它的值:
|
||||
|
||||
$ echo The value of variable x = $x
|
||||
|
||||
The value of variable x = 10
|
||||
|
||||
**Note:** The ‘**-e**‘ option in Linux acts as interpretation of escaped characters that are backslashed.
|
||||
**注意:** Linux中的选项‘**-e**‘扮演了转义字符反斜线的翻译器。
|
||||
|
||||
**3.** Using option ‘**\b**‘ – backspace with backslash interpretor ‘**-e**‘ which removes all the spaces in between.
|
||||
**3.** 使用‘**\b**‘选项- ‘**-e**‘后带上'\b'会删除字符间的所有空格。
|
||||
|
||||
$ echo -e "Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds"
|
||||
|
||||
TecmintisacommunityofLinuxNerds
|
||||
|
||||
**4.** Using option ‘**\n**‘ – New line with backspace interpretor ‘**-e**‘ treats new line from where it is used.
|
||||
**4.** 使用‘**\n**‘选项- ‘**-e**‘后面的带上‘\n’行会在遇到的地方作为新的一行
|
||||
|
||||
$ echo -e "Tecmint \nis \na \ncommunity \nof \nLinux \nNerds"
|
||||
|
||||
@ -50,13 +47,13 @@ echo its value:
|
||||
Linux
|
||||
Nerds
|
||||
|
||||
**5.** Using option ‘**\t**‘ – horizontal tab with backspace interpretor ‘**-e**‘ to have horizontal tab spaces.
|
||||
**5.** 使用‘**\t**‘选项 - ‘**-e**‘后面跟上‘\t’会在空格间加上水平制表符。
|
||||
|
||||
$ echo -e "Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds"
|
||||
|
||||
Tecmint is a community of Linux Nerds
|
||||
|
||||
**6.** How about using option new Line ‘**\n**‘ and horizontal tab ‘**\t**‘ simultaneously.
|
||||
**6.** 也可以同时使用换行‘**\n**‘与水平制表符‘**\t**‘。
|
||||
|
||||
$ echo -e "\n\tTecmint \n\tis \n\ta \n\tcommunity \n\tof \n\tLinux \n\tNerds"
|
||||
|
||||
@ -68,7 +65,7 @@ echo its value:
|
||||
Linux
|
||||
Nerds
|
||||
|
||||
**7.** Using option ‘**\v**‘ – vertical tab with backspace interpretor ‘**-e**‘ to have vertical tab spaces.
|
||||
**7.** 使用‘**\v**‘选项 - ‘**-e**‘后面跟上‘\v’会加上垂直制表符。
|
||||
|
||||
$ echo -e "\vTecmint \vis \va \vcommunity \vof \vLinux \vNerds"
|
||||
|
||||
@ -80,7 +77,7 @@ echo its value:
|
||||
Linux
|
||||
Nerds
|
||||
|
||||
**8.** How about using option new Line ‘**\n**‘ and vertical tab ‘**\v**‘ simultaneously.
|
||||
**8.** 也可以同时使用换行‘**\n**‘与垂直制表符‘**\v**‘。
|
||||
|
||||
$ echo -e "\n\vTecmint \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds"
|
||||
|
||||
@ -99,45 +96,45 @@ echo its value:
|
||||
|
||||
Nerds
|
||||
|
||||
**Note:** We can double the vertical tab, horizontal tab and new line spacing using the option two times or as many times as required.
|
||||
**注意:** 你可以按照你的需求连续使用两个或者多个垂直制表符,水平制表符与换行符。
|
||||
|
||||
**9.** Using option ‘**\r**‘ – carriage return with backspace interpretor ‘**-e**‘ to have specified carriage return in output.
|
||||
**9.** 使用‘**\r**‘选项 - ‘**-e**‘后面跟上‘\r’来指定输出中的回车符。
|
||||
|
||||
$ echo -e "Tecmint \ris a community of Linux Nerds"
|
||||
|
||||
is a community of Linux Nerds
|
||||
|
||||
**10.** Using option ‘**\c**‘ – suppress trailing new line with backspace interpretor ‘**-e**‘ to continue without emitting new line.
|
||||
**10.** 使用‘**\c**‘选项 - ‘**-e**‘后面跟上‘\c’会抑制输出后面的字符并且最后不会换新行。
|
||||
|
||||
$ echo -e "Tecmint is a community \cof Linux Nerds"
|
||||
|
||||
Tecmint is a community avi@tecmint:~$
|
||||
Tecmint is a community @tecmint:~$
|
||||
|
||||
**11.** Omit echoing trailing new line using option ‘**-n**‘.
|
||||
**11.** ‘**-n**‘会在echo完后不会输出新行。
|
||||
|
||||
$ echo -n "Tecmint is a community of Linux Nerds"
|
||||
Tecmint is a community of Linux Nerdsavi@tecmint:~/Documents$
|
||||
Tecmint is a community of Linux Nerds@tecmint:~/Documents$
|
||||
|
||||
**12.** Using option ‘**\a**‘ – alert return with backspace interpretor ‘**-e**‘ to have sound alert.
|
||||
**12.** 使用‘**\c**‘选项 - ‘**-e**‘后面跟上‘\a’选项会听到声音警告。
|
||||
|
||||
$ echo -e "Tecmint is a community of \aLinux Nerds"
|
||||
Tecmint is a community of Linux Nerds
|
||||
|
||||
**Note:** Make sure to check Volume key, before firing.
|
||||
**注意:** 在你开始前,请先检查你的音量键。
|
||||
|
||||
**13.** Print all the files/folder using echo command (ls command alternative).
|
||||
**13.** 使用echo命令打印所有的文件和文件夹(ls命令的替代)。
|
||||
|
||||
$ 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 files of a specific kind. For example, let’s assume you want to print all ‘**.jpeg**‘ files, use the following command.
|
||||
**14.** 打印制定的文件类型。比如,让我们假设你想要打印所有的‘**.jpeg**‘文件,使用下面的命令。
|
||||
|
||||
$ echo *.jpeg
|
||||
|
||||
network.jpeg
|
||||
|
||||
**15.** The echo can be used with redirect operator to output to a file and not standard output.
|
||||
**15.** echo可以使用重定向符来输出到一个文件而不是标准输出。
|
||||
|
||||
$ echo "Test Page" > testpage
|
||||
|
||||
@ -145,58 +142,58 @@ echo its value:
|
||||
avi@tecmint:~$ cat testpage
|
||||
Test Page
|
||||
|
||||
### echo Options ###
|
||||
### echo 选项 ###
|
||||
|
||||
<table border="0" cellspacing="0">
|
||||
<colgroup width="85"></colgroup>
|
||||
<colgroup width="271"></colgroup>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> Options</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"><b> Description</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> 选项</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"><b> 描述</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> -n</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> do not print the trailing newline.</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 不输出末尾的换行符。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> -e</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> enable interpretation of backslash escapes.</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 启用反斜线转义。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> \b</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> backspace</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 退格</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> \\</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> backslash</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 反斜线</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> \n</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> new line</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 新行</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> \r</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> carriage return</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 回车</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> \t</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> horizontal tab</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 水平制表符</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid #000000;" align="LEFT" height="16"><b> \v</b></td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> vertical tab</td>
|
||||
<td style="border: 1px solid #000000;" align="LEFT"> 垂直制表符</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
That’s all for now and don’t forget to provide us with your valuable feedback in the comments below.
|
||||
就是这些了,不要忘记在下面留下你有价值的反馈。
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/echo-command-in-linux/
|
||||
|
||||
作者:[Avishek Kumar][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user