终端打印

实战演练

  • 特殊符号(!),在bash上打印
[root@shaofeng ~]# echo "!"
-bash: !: event not found
[root@shaofeng ~]# echo "\!"
\!
[root@shaofeng ~]# echo '!'
!
[root@shaofeng ~]# echo !
!
  • 特殊符号(!),在zsh上打印
➜  ~ git:(master) ✗ echo "Welcome zsh \!"
Welcome zsh !
➜  ~ git:(master) ✗ echo "!"
> "
  • 格式化打印
[root@shaofeng ~]# ./printf.sh
No    Name       Mark
1     Sarath     80.35
[root@shaofeng ~]# cat printf.sh
#!/bin/bash

printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456

工作原理

  • %s、%c、%d和%f都是格式替换符(format subsitution character)
    • %-5s 占位5 s字符串
    • %-4.2f .2指定保留2个小数位
    • \n 换行符
    • \0 null字符

补充内容

  • 字体颜色:重置=0,黑色=30,红色=31,绿色=32,黄色=33,蓝色=34,洋红=35,青色=36,白色=37
  • 背景色:重置=0,黑色=40,红色=41,绿色=42,黄色=43,蓝色=44,洋红=45,青色=46,白色=47
[root@shaofeng ~]# echo -e "\e[1;31m This is red text \e[0m"
 This is red text
[root@shaofeng ~]# echo -e "\e[1;42m Green Background \e[0m"
 Green Background
[root@shaofeng ~]# echo -e "1\t2\t3"
1       2       3

results matching ""

    No results matching ""