[student@myhost ~]$ echo h h h
h h h
[student@myhost ~]$ echo "h h h"
h h h
[student@myhost ~]$ echo 'h h h'
h h h
我们可以看到echo命令执行完后自带一个换行符并且双引号引起来仅打印出引号里的内容。
如果不想echo换行,用-n选项。
[student@myhost ~]$ echo "h h h!"
bash: !": event not found
[student@myhost ~]$ echo h h h! #无法打印;
h h h!
[student@myhost ~]$ echo 'h h h!'
h h h!
以上我们知道!无法在双引号“”中打印。
[student@myhost ~]$ echo -e "1\t2\t"
1 2
[student@myhost ~]$ echo "1\t2\t"
1\t2\t
以上我们知道echo输出转义序列时要加选项-e。
[student@myhost ~]$ echo -e "\e[1;31mh h h\e[0m" #\e[1;31m代表将颜色设为红色,\e[0m代表将颜色置回。
h h h
以上我们知道如何打印彩色字体,其中31为红,0为重置,30为黑,32为绿,33为黄,34为蓝,35为洋红,36为青,37为白。字体颜色+10的值即为背景颜色。
[student@myhost ~]$ cat a
printf "%-5s %-10s %-4s\n" No Name Mark #其中%s为字符串格式替换符,-代表左对齐,5代表宽度
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456 #其中%f为浮点数替换符,-代表左对齐,.2保留两位小数
[student@myhost ~]$ ./a
No Name Mark
1 Sarath 80.35
printf可以控制输出格式,但要手动换行\n。