echo
可以使用echo
对字符串进行打印,根据需求善用双引号和单引号。
注意
:echo 输出是自带换行的
#!/bin/bash
name='LiLei'
echo "Your name is LiLei"
echo "Your name is $name"
echo "\"Your name is $name\""
echo 'Your name is LiLei'
echo 'Your name is $name'
#sh test.sh
Your name is LiLei
Your name is LiLei
"Your name is LiLei"
Your name is LiLei
Your name is $name
- 开启转义
-e
,\n
表示换行,\c
表示不换行。
#!/bin/bash
echo "Your name is LiLei\n"
echo -e "Your name is LiLei\n"
echo "Your name is LiLei\c"
echo -e "Your name is LiLei\c"
#sh test.sh
Your name is LiLei\n
Your name is LiLei
Your name is LiLei\c
Your name is LiLei$
- 结果重定向, 符号
>
#!/bin/bash
echo "Your name is LiLei\n" >name.txt
# result
$ cat name.txt
Your name is LiLei\n
- 打印执行命令结果, 注意特殊符号 `
#!/bin/bash
echo `date`
#sh test.sh
Mon Jul 15 10:42:41 CST 2019
printf
可以使用printf
对字符串进行格式化
打印。
注意:printf
打印不会自动换行, 换行必须显式添加换行符(\n)
网友评论