1、Shell echo命令
用于字符串的输出:echo string
显示普通字符串:echo "It is a test" 等同于 echo It is a test,
结果是 It is a test
显示转义字符:echo "\"It is a test\""
结果是"It is a test"
显示变量:read命令从标准输入中读取一行,并把输入行的每个字段的值指定给shell变量
sh文件里:read name
echo "$name It is a test"
输入命令行:OK 输入
OK It is a test 输出
显示换行:\n
echo -e "OK ! \n" -e开启转义,\n换行
echo "It is a test"
显示不换行:
echo -e "OK ! \c" -e开启转义, \c不换行
echo "It is a test"
显示结果定向至文件:echo "It is a test" >myfile
原样输出字符串,不进行转义或取变量(用单引号):echo '$name\"' 结果为:$name\"
显示命令执行结果:echo `data`,结果显示的是当前的日期
2、Shell printf命令
printf不会自动添加换行符,可以手动添加\n
语法:printf format-string [arguments...] 格式控制字符串;参数列表
printf中%s,%c,%d都是格式替代符,%-10s表示一个宽度为10个字符(-表示左对齐,没有则默认为右对齐),任何字符也会显示在10个字符宽的字符内,如果不足则自动以空格添加,超过也会显示出来;%-4.2f指格式化为小数,.2指保留两位小数
例如
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg 结果为:姓名 性别 体重kg
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 结果为:郭靖 男 66.12
3、shell test命令
数值测试:https://mp.csdn.net/postedit/81298736