shell 程式設計 9 printf

2021-10-04 04:57:48 字數 935 閱讀 2903

仿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 *** height

tom male 160.33

jack female 178.999

[root@k8s-master test5]# cat t0.sh

#!/bin/bash

printf "%-15s %-8s %-5s\n" name *** 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]#

Shell程式設計中的printf

上一章節我們學習了 shell 的 echo 命令,本章節我們來學習 shell 的另乙個輸出命令 printf。printf 命令模仿 c 程式庫 library 裡的 printf 程式。printf 由 posix 標準所定義,因此使用 printf 的指令碼比使用 echo 移植性好。pri...

shell程式設計之printf指令篇

bin bash 格式 printf format string arguments.引數說明 format string 為格式控制字串 arguments 為引數列表 printf 10s 8s 4s n 姓名 性別 體重kg printf 10s 8s 4.2f n 郭靖 男 66.1234 ...

34 shell程式設計 awk之printf命令

格式符含義 s列印字串 d列印十進位制數 f列印浮點數 x列印十六進製制數 o列印八進位制數 e列印數字的科學計數法形式 c列印單子字元的ascii碼 左對齊 右對齊 1.已字串的格式列印 etc passwd的第7個字段,已 作為分隔符號 awk begin etc passwd 2.已10進製的...