1. echo命令的定义和语法
echo 是一个 Shell 内建命令,用来在终端输出字符串
[root@server1 mnt]# sh test.sh
students,你好!
http://www.westos.org
Shell教程的网址是:http://www.westos.org
${name}的网址是:${url}
[root@server1 mnt]# cat test.sh
#!/bin/bash
name="Shell教程"
url="http://www.westos.org"
echo "students,你好!" #直接输出字符串
echo $url #输出变量
echo "${name}的网址是:${url}" #双引号包围的字符串中可以解析变量
echo '${name}的网址是:${url}' #单引号包围的字符串中不能解析变量
2. 不换行
[root@server1 mnt]# sh test.sh
Tom is 20 years old, 175cm in height and 62kg in weight.
Thank you!
[root@server1 mnt]# cat test.sh
#!/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!"
3. 输出转义字符
默认情况下,echo 不会解析以反斜杠\开头的转义字符。比如,\n表示换行,echo 默认会将它作为普通字符对待
[root@server1 mnt]# echo "hello \nworld"
hello \nworld
[root@server1 mnt]# echo -e "hello \nworld"
hello
world
有了-e参数,我们也可以使用转义字符\c来强制 echo 命令不换行了
[root@server1 mnt]# sh test.sh
Tom is 20 years old, 175cm in height and 62kg in weight.
Thank you!
[root@server1 mnt]# cat test.sh
#!/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!"