Shell编程四-Shell echo命令

Shell echo命令

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

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

1.显示普通字符串:
echo "C++ is the best language!"

这里的双引号完全可以省略,以下命令与上面实例效果一致:
echo C++ is the best language!

2.显示转义字符:
echo \"C++ is the best language!\"
或者:
echo "\"C++ is the best language!\""

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

4.显示换行
echo -e "\"C++ is the best language!\" \n"

5.#显示不换行
echo -e "\"C++ is the best language!\" \c"
echo "abc"

6.显示结果定向至文件
echo "C++ is the best language!" > file.txt

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

8.显示命令执行结果
echo `date`

实例:
#!/bin/bash
#--------------------------------------------
#name:practise20.sh
#author:wdh
#date:20181207
#--------------------------------------------

#显示普通字符串
echo "C++ is the best language!"

#省略双引号
echo C++ is the best language!

#显示转义字符
echo \"C++ is the best language!\"

#显示变量
echo "Please enter a line of characters:"
read name

#显示换行
#-e 转义开启
echo -e "\"C++ is the best language!\" \n$name"

#显示不换行
#\c 不换行
echo -e "\"C++ is the best language!\" \c"
echo "$name"

#显示结果定向至文件
echo "C++ is the best language!" > file.txt

#原样输出字符串,不进行转义或取变量(用单引号)
echo '#name\"'

#显示命令执行结果
echo `date`
#!/bin/bash
#practise20.sh

#显示普通字符串
echo "C++ is the best language!"

#省略双引号
echo C++ is the best language!

#显示转义字符
echo \"C++ is the best language!\"

#显示变量
echo "Please enter a line of characters:"
read name

#显示换行
#-e 转义开启
echo -e "\"C++ is the best language!\" \n$name"

#显示不换行
#\c 不换行
echo -e "\"C++ is the best language!\" \c"
echo "$name"

#显示结果定向至文件
echo "C++ is the best language!" > file.txt

#原样输出字符串,不进行转义或取变量(用单引号)
echo '#name\"'

#显示命令执行结果
echo `date`

执行 ./practise20.sh
运行脚本,结果如下:
C++ is the best language!
C++ is the best language!
"C++ is the best language!"
Please enter a line of characters:
abcd
"C++ is the best language!" 
abcd
"C++ is the best language!" abcd
#name\"
2018年 12月 07日 星期五 21:04:42 CST
 

猜你喜欢

转载自blog.csdn.net/weidonghua2/article/details/84887227