15 Practical Examples of ‘echo’ command in Linux

By Avishek Kumar Under: Linux Commands On: August 21, 2014

 
 
 
翻译 by SmallLiu

echo命令是linux中的bash shell和C shell最常用的内建命令,最典型的是在脚本和批处理文件中的使用。其作用是在标准输出或者文件中显示一行文本。

echo command examples

echo命令的语法形式如下:

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

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

$ echo Tecmint is a community of Linux Nerds 

其输出结果如下:

Tecmint is a community of Linux Nerds 

2.声明一个变量并通过echo获取其值.例如,如下就是声明一个x变量并且将其值赋为10.

$ x=10

通过echo命令获取其值:

$ echo The value of variable x = $x 

The value of variable x = 10 

Note:'-e'选项将转义字符作特殊处理

3.使用'\b'选项将移除\b前面一个字符

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

TecmintisacommunityofLinuxNerds 

4.使用'\n'选项将产生新的换行

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

Tecmint
is
a
community
of
Linux
Nerds

5.使用'\t'将产生一个制表符

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

Tecmint 	is 	a 	community 	of 	Linux 	Nerds 

6.同时使用\n与\t的结果如下:

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

	Tecmint
is
a
community
of
Linux
Nerds

7.'\v'选项,换行但是光标仍停留在原处

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

Tecmint
is
a
community
of
Linux
Nerds

8.'\n'和'\v'选项将产生两个换行

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

Tecmint 

is 

a 

community 

of 

Linux 

Nerds 

9.‘\r’选项,该选项之前的文本被忽略.

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

is a community of Linux Nerds 

10.'\c'选项,忽略\c后面的文本,并且不输出换行

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

Tecmint is a community avi@tecmint:~$ 

11. ‘-n’选项输出文本后不输出换行

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

12.使用'\a'选项发出警告

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

13. 显示所有的文件和文件夹

$ 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. 显示指定格式的文件

$ echo *.jpeg 

network.jpeg

15. 将echo回显的内容重定向到testpage文件

$ echo "Test Page" > testpage 

## Check Content
avi@tecmint:~$ cat testpage
Test Page
echo 命令选项
 Options  Description
 -n  do not print the trailing newline.//输出不换行
 -e  enable interpretation of backslash escapes.//解释转义字符
 \b  backspace  //删除前一个字符
 \\  backslash 插入\字符
 \n  new line    //新的换行符
 \r  carriage return //光标移至首行
 \t  horizontal tab  //制表符
 \v  vertical tab  //换行光标位置不变

以上即是所有关于echo的内容,有什么问题和意见请在评论栏反馈。

(译)linux系统关于命令echo的15个例子的更多相关文章

  1. 查看linux系统版本命令 (转)

    查看linux系统版本命令 分类: Linux 知识小结2011-10-10 15:26 240162人阅读 评论(9) 收藏 举报 linuxredhatdebianx86susesun 一.查看内 ...

  2. Linux系统基础命令

    这是看itercast的学习笔记 Linux系统基础命令 日期时间 命令date用以查看.设置当前系统时间:格式化显示时间: +%Y--%m--%d 命令hwclock(clock)用以显示硬件时钟时 ...

  3. Linux系统-解压缩命令集合

    Linux系统-解压缩命令集合 linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. ...

  4. 给大家介绍一下linux系统高级命令

    输出重定向(mip.0834jl.com) ;覆盖文件内容 回声' 123 ' 测试;覆盖原始内容 回声' 123 ' 测试;原始存在(共存) echo 'ken2 '(www.jl0834.com) ...

  5. Linux系统ifconfig命令找不到,centos ifconfig Command not found

    centos ifconfig Command not found,Linux系统ifconfig命令找不到 >>>>>>>>>>>& ...

  6. LINUX系统VMSTAT命令详解

    linux系统vmstat命令详解 [转自 https://www.cnblogs.com/wensiyang0916/p/6514820.html] vmstat 1    1表示每秒采集一次vms ...

  7. 【转载】Linux系统下命令行连接蓝牙设备 查看查找 蓝牙

    Linux系统下命令行连接蓝牙设备 2018年11月26日 10:47:27 Zz笑对一切 阅读数:741   1.打开系统蓝牙 sudo service bluetooth start 1 进入bl ...

  8. Linux系统——awk命令

    awk命令不仅仅是Linux系统的命令,也是一种编程语言,用来处理数据和生成报告(Exel),处理的数据可以是一个或多个文件(标准输入和管道获取标准输入).可在命令行上编辑操作,也可以写成awk程序运 ...

  9. spark实验(一)--linux系统常见命令及其文件互传(2)

    2.使用 Linux 系统的常用命令 启动 Linux 虚拟机,进入 Linux 系统,通过查阅相关 Linux 书籍和网络资料,或者参考 本教程官网的“实验指南”的“Linux 系统常用命令”,完成 ...

随机推荐

  1. Linux远程桌面

    发现windows下有个VNC viewer.据说是跨平台的. sudo apt-cache search VNC | grep VNC了一下.发现有一个gvncviewer的包,然后apt-get ...

  2. Spring Task Scheduler - No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined

    1. Overview In this article, we are discussing the Springorg.springframework.beans.factory.NoSuchBea ...

  3. hdu4393 Throw nails(只用模拟前面500来次,后面根据速度、位置、id值排序即可)

                                                                                                         ...

  4. LeeCode-Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. hibernate多对一的操作解析

    在hibernate的关联操作中有很多关系,其中多对一关系是最常见的.我们看这两个表. 这里有部门表和员工表. 那么我们可以这么说一个部门可以有多个员工.这就是1对多的关系.这是我们站在部门表的角度上 ...

  6. PHP MySQL Insert Into 之 Insert

    向数据库表插入数据 INSERT INTO 语句用于向数据库表添加新记录. 语法 INSERT INTO table_name VALUES (value1, value2,....) 您还可以规定希 ...

  7. [LeetCode] Interleaving String [30]

    题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...

  8. 从决策树学习谈到贝叶斯分类算法、EM、HMM

    从决策树学习谈到贝叶斯分类算法.EM.HMM                (Machine Learning & Recommend Search交流新群:172114338) 引言 log ...

  9. LFS:kernel panic VFS: Unable to mount root fs

    说明: 使用Vm虚拟机构建自己的LFS系统时,系统引导不成功,提示 kernel panic VFS: Unable to mount root fs 参考链接:http://www.52os.net ...

  10. AppSettings

    1.winform中读写配置文件appSettings 一节中的配置. #region 读写配置文件 /// <summary> /// 修改配置文件中某项的值 /// </summ ...