linux菜鸟入门echo命令的15个实际示例

2023-02-1317:06:12服务器及运维Comments1,510 views字数 2380阅读模式

echoLinux bash和C shell中最常用和最广泛使用的内置命令之一。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html

echo的语法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html

echo [option(s)] [string(s)]

1.输入一行文本并显示在标准输出上

$ echo rumenz is a community of Linux Nerds

输出以下文本:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html

rumenz is a community of Linux Nerds

2.声明一个变量并打印它的值。例如:声明一个变量x并分配其值 =10.

$ x=10

打印:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html

$ echo The value of variable x = $x 

The value of variable x = 10

Note:这 -e Linux 中的选项充当反斜杠转义字符的解释。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html

3.使用选项\b 退格符和反斜杠解释器-e,删除中间的所有空格。

$ echo -e "rumenz \bis \ba \bcommunity \bof \bLinux \bNerds" 

rumenzisacommunityofLinuxNerds

4.使用选项\n-e换行输出。

$ echo -e "rumenz \nis \na \ncommunity \nof \nLinux \nNerds" 

rumenz 
is 
a 
community 
of 
Linux 
Nerds

5.使用选项 \t水平制表符和-e

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

rumenz  is  a  community  of  Linux  Nerds

6.同时使用选项\n和水平制表符\t

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


        rumenz 
        is 
        a 
        community 
        of 
        Linux 
        Nerds

7.使用选项 \v垂直制表符和-e

$ echo -e "\vrumenz \vis \va \vcommunity \vof \vLinux \vNerds" 

rumenz 
        is 
           a 
             community 
                       of 
                          Linux 
                                Nerds

8.同时使用选项\n和垂直制表符\v

$ echo -e "\n\vrumenz \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds" 


rumenz 

is 

a 

community 

of 

Linux 

Nerds

Note:我们可以使用该选项两次或根据需要多次将垂直制表符、水平制表符和新行间距加倍。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html

9.使用选项 \r-e光标移至行首,但不换行。

$ echo -e "rumenz \ris a community of Linux Nerds" 

is a community of Linux Nerds

10.使用选项 \c-e不换行输出,\c后面的字符不显示。

$ echo -e "rumenz is a community \cof Linux Nerds" 

rumenz is a community rumenz@local:~$

11.使用-n不输出末尾的换行符。

$ echo -n "rumenz is a community of Linux Nerds" 
rumenz is a community of Linux Nerdsrumenz@local:~/Documents$

12.使用选项\a带有退格解释器的警报返回-e要有声音警报。

$ echo -e "rumenz is a community of \aLinux Nerds" 
rumenz is a community of Linux Nerds

Note:确保在发射前检查音量键。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html

13.使用echo命令打印所有文件。

$ 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.打印所有.jpeg文件,请使用以下命令。

$ echo *.jpeg 

network.jpeg

15.echo可以与重定向运算符一起使用以输出到文件而不是标准输出。

$ echo "rumenz.com" > 1.txt 


rumenz@local:~$ cat 1.txt 
rumenz.com
echo选项
OptionsDescription
-ndo not print the trailing newline.
-eenable interpretation of backslash escapes.
\bbackspace
\backslash
\nnew line
\rcarriage return
\thorizontal tab
\vvertical tab
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30860.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/30860.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定