shell echo

echo的基本功能: 字符输出

  1. 显示字符串:
echo It is a test
# 等价于
echo “It is a test“
  1. 显示转义字符
echo "\"It is a test\""
## 等价于
echo  \"It is a test\"

结果是:

"It is a test"
  1. 显示变量

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

例子:

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

结果:
测试
4. 换行和不换行

  • 换行 \n
  • 不换行 \c
  • -e 在命令行中表示开启转义

换行例子:

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

结果:

OK!

It is a test

不换行例子

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

结果:

OK! It is a test
  1. 显示结果定向至文件
 echo "It is a test" > myfile
  1. .显示命令执行结果
echo `date`

这里使用的是反引号 `, 而不是单引号 '。
结果:
2019年 12月 29日 星期日 16:38:03 CST

原创文章 9 获赞 6 访问量 4553

猜你喜欢

转载自blog.csdn.net/qq_38870935/article/details/103754617