shell 編程 (9)printf

仿C程序的printf()程序

printf format-string [arguments...]

format-string用雙引號或單引號括起來,如果只有1個輸出可不用引號

printf需手動添加\n才能起到換行效果。echo會自動添加換行符。

%-ns:寬度爲n個字符,-左對齊,否則右對齊,不足用空格,超過全部顯示

%m.nf:整數m位,小數n位。

%d:整數

無對應參數%s爲NULL, %d爲0

eg:

[root@k8s-master test3]# echo "helloworld"
helloworld
[root@k8s-master test3]# printf "helloworld"
helloworld[root@k8s-master test3]# printf "helloworld\n"
helloworld
[root@k8s-master test3]# 

[root@k8s-master test5]# ./t0.sh 
name            sex      height
tom             male     160.33
jack            female   178.999
[root@k8s-master test5]# cat t0.sh 
#!/bin/bash

printf "%-15s %-8s %-5s\n" name sex height
printf "%-15s %-8s %-5.2f\n" tom male 160.333
printf "%-15s %-8s %-5.3f\n" jack female 178.999
[root@k8s-master test5]# 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章