【Shell脚本7】Shell echo命令

编程入门 行业动态 更新时间:2024-10-25 17:26:43

【Shell<a href=https://www.elefans.com/category/jswz/34/1771291.html style=脚本7】Shell echo命令"/>

【Shell脚本7】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.显示变量

ead 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 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`注意: 这里使用的是反引号 `, 而不是单引号 '。结果将显示当前日期2023年 11月 01日 星期三 09:29:54 CST

更多推荐

【Shell脚本7】Shell echo命令

本文发布于:2023-11-15 21:27:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   命令   Shell   echo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!