参考菜鸟教程:https://www.runoob.com/linux/linux-shell-echo.html
1、显示换行:
echo -e "OK! \n" # -e 开启转义
echo "It is a test"
得到的结果为,
OK!
It is a test
2、显示不换行
#!/bin/sh
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"
得到的结果为,
OK! It is a test
3、显示结果定向至文件
命令行中输入,如,
echo "It is a test" > myfile
使用vim查看到It is a test
4、原样输出字符串,外层使用单引号
echo '$name\"'
输出结果为,$name\"
5、显示命令执行结果
echo `date` # 这里使用的是反引号 ` !!!
显示结果为,
Wed Jul 31 11:06:54 CST 2019
参考菜鸟教程:https://www.runoob.com/linux/linux-shell-printf.html
printf 命令的语法: printf format-string [arguments...]
参数说明:format-string: 为格式控制字符串
arguments: 为参数列表
默认 printf 不会像 echo 自动添加换行符,我们可以手动添加 \n
例如,
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234
printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876
注: %-10s 指一个宽度为10个字符(-表示左对齐,没有则表示右对齐),任何字符都会被显示在10个字符宽的字符内,如果不足则自动以空格填充,超过也会将内容全部显示出来。
未完,剩余参考菜鸟教程。