好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Shell echo命令

Shell echo命令

Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出。命令格式:

echo string

您可以使用echo实现更复杂的输出格式控制。

1.显示普通字符串:

echo "It is a test"

这里的双引号完全可以省略,以下命令与上面实例效果一致:

echo It is a test

2.显示转义字符

echo "\"It is a test\""

结果将是:

"It is a test"

同样,双引号也可以省略

3.显示变量

read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量

#!/bin/sh
read name 
echo "$name It is a test"

以上代码保存为 test.sh,name 接收标准输入的变量,结果将是:

[root@www ~]# sh test.sh
OK                     #标准输入
OK It is a test        #输出

4.显示换行

echo -e "OK! \n" # -e 开启转义
echo "It is a test"

输出结果:

OK!

It is a test

5.显示不换行

#!/bin/sh
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"

输出结果:

OK! It is a test

6.显示结果定向至文件

echo "It is a test" > myfile

7.原样输出字符串,不进行转义或取变量(用单引号)

echo '$name\"'

输出结果:

$name\"

8.显示命令执行结果

echo `date`

注意: 这里使用的是反引号 ` , 而不是单引号 ' 。

结果将显示当前日期

Thu Jul 24 10:08:46 CST 2014

查看更多关于Shell echo命令的详细内容...

  阅读:31次

上一篇

下一篇

第1节:Linux vi/vim    第2节:linux yum 命令    第3节:Linux 简介    第4节:Linux 安装    第5节:Linux 教程    第6节:Linux 磁盘管理    第7节:Linux 系统启动过程    第8节:Linux 系统目录结构    第9节:Linux 忘记密码解决方法    第10节:Linux 远程登录    第11节:Linux 文件基本属性    第12节:Linux 文件与目录管理    第13节:Linux 用户和用户组管理    第14节:Linux 命令大全    第15节:Shell 教程    第16节:Shell 变量    第17节:Shell echo命令    第18节:MySQL 安装配置    第19节:Shell test 命令    第20节:Shell 流程控制    第21节:Shell 函数    第22节:Nginx 安装配置