shell中的 echo命令

Shell echo命令

echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串。命令格式:

#所见即所得
echo arg

#echo是bash解释器的内建命令
[root@hass-11 ~]# type echo
echo is a shell builtin

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

显示转义的字符

echo ""It is a test""
"It is a test"

echo -e "${color}txt${res}"

echo -e "OK! 
It is a test"

双引号也可以省略。

显示变量

name="OK"
echo "$name It is a test"
OK It is a test

#同样双引号也可以省略

如果变量与其它字符相连的话,需要使用大括号{ }指定变量的界限:

mouth=8
echo "${mouth}-1-2009"

结果将是:
8-1-2009

显示换行

echo -e "OK! 
It is a test"

输出:
OK!
It is a test

显示不换行

echo -n "OK! It is a test"
for ((i=0;i < 5;i++));do echo -n ".";sleep 1;done

#不换行输出

输出:
OK!It si a test

显示结果重定向至文件

echo "It is a test" > myfile

原样输出字符串

若需要原样输出字符串(不进行转义),请使用单引号。例如:

echo '$name"'

显示命令执行结果

#优先执行 `` 内的命令
echo `date`

结果将显示当前日期

从上面可看出,双引号可有可无,单引号主要用在原样输出中。

原文地址:https://www.cnblogs.com/syy1757528181/p/13603731.html