Shell echo令:输出字符串
echo 是一个 Shell 内建令,用来在终端输出字符串,并在默认加上换行符。请看下面的例子:
#!/bin/bash
name="Shell教程"
url="http://c.biancheng网站站点" rel="nofollow" />
读者,你好!
http://c.biancheng网站站点" rel="nofollow" />#!/bin/bash
name="Tom"
age=20
height=175
weight=62
echo -n "${name} is ${age} years old, "
echo -n "${height}cm in height "
echo "and ${weight}kg in weight."
echo "Thank you!"
运行结果:
Tom is 20 years old, 175cm in height and 62kg in weight.
Thank you!
输出转义字符
默认情况下,echo 不会解析以反斜杠\
开头的转义字符。比如,\n
表示换行,echo 默认会将它作为普通字符对待。请看下面的例子:[root@localhost ~]# echo "hello \nworld"
hello \nworld
我们可以添加-e
参数来让 echo 令解析转义字符。例如:[root@localhost ~]# echo -e "hello \nworld"
hello
world
\c 转义字符
有了-e
参数,我们也可以使用转义字符\c
来强制 echo 令不换行了。请看下面的例子:#!/bin/bash
name="Tom"
age=20
height=175
weight=62
echo -e "${name} is ${age} years old, \c"
echo -e "${height}cm in height \c"
echo "and ${weight}kg in weight."
echo "Thank you!"
运行结果:
Tom is 20 years old, 175cm in height and 62kg in weight.
Thank you!