{"id":4020,"date":"2019-07-18T18:50:37","date_gmt":"2019-07-18T10:50:37","guid":{"rendered":"https:\/\/www.linuxcool.com\/?p=4020"},"modified":"2019-07-18T18:50:40","modified_gmt":"2019-07-18T10:50:40","slug":"printf","status":"publish","type":"post","link":"https:\/\/www.linuxcool.com\/printf","title":{"rendered":"printf \u547d\u4ee4 – shell \u8f93\u51fa"},"content":{"rendered":"\n

printf \u547d\u4ee4\u6a21\u4eff C \u7a0b\u5e8f\u5e93\uff08library\uff09\u91cc\u7684 printf() \u7a0b\u5e8f\u3002<\/p>\n\n\n\n

printf \u4f7f\u7528\u5f15\u7528\u6587\u672c\u6216\u7a7a\u683c\u5206\u9694\u7684\u53c2\u6570\uff0c\u5916\u9762\u53ef\u4ee5\u5728printf\u4e2d\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u5236\u5b9a\u5b57\u7b26\u4e32\u7684\u5bbd\u5ea6\u3001\u5de6\u53f3\u5bf9\u9f50\u65b9\u5f0f\u7b49\u3002\u9ed8\u8ba4printf\u4e0d\u4f1a\u50cf echo \u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u6dfb\u52a0 \\n\u3002<\/p>\n\n\n\n

\u8bed\u6cd5\u683c\u5f0f<\/strong>\uff1a printf [\u683c\u5f0f\u63a7\u5236\u5b57\u7b26\u4e32] [\u53c2\u6570]<\/p>\n\n\n\n

\u5e38\u7528\u53c2\u6570<\/strong>\uff1a <\/p>\n\n\n\n
\\a <\/td>\u8b66\u544a\u5b57\u7b26\uff0c\u901a\u5e38\u4e3aASCII\u7684BEL\u5b57\u7b26 <\/td><\/tr>
\\n <\/td>\u6362\u884c<\/td><\/tr>
\\r <\/td>\u56de\u8f66<\/td><\/tr>
\\0ddd <\/td>\u8868\u793a1\u52303\u4f4d\u7684\u516b\u8fdb\u5236\u503c\u5b57\u7b26 <\/td><\/tr>
\\ddd <\/td>\u8868\u793a1\u52303\u4f4d\u6570\u516b\u8fdb\u5236\u503c\u7684\u5b57\u7b26\u3002\u4ec5\u5728\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\u6709\u6548 <\/td><\/tr><\/tbody><\/table>\n\n\n\n

\u53c2\u8003\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n

\u5b57\u7b26\u4e32\u8f93\u51fa\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# printf \"Hello, Shell\\n\" \nHello, Shell <\/pre>\n\n\n\n

\u63a5\u4e0b\u6765,\u6211\u6765\u7528\u4e00\u4e2a\u811a\u672c\u6765\u4f53\u73b0printf\u7684\u5f3a\u5927\u529f\u80fd\uff1a <\/p>\n\n\n\n

[root@linuxcool ~]# \nprintf \"%-10s %-8s %-4s\\n\" \u59d3\u540d \u6027\u522b \u4f53\u91cdkg\nprintf \"%-10s %-8s %-4.2f\\n\" \u90ed\u9756 \u7537 66.1234  \nprintf \"%-10s %-8s %-4.2f\\n\" \u6768\u8fc7 \u7537 48.6543  \nprintf \"%-10s %-8s %-4.2f\\n\" \u90ed\u8299 \u5973 47.9876 <\/pre>\n\n\n\n

\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# \n\u59d3\u540d     \u6027\u522b   \u4f53\u91cdkg \n\u90ed\u9756     \u7537      66.12 \n\u6768\u8fc7     \u7537      48.65 \n\u90ed\u8299     \u5973      47.99 <\/pre>\n\n\n\n

%s %c %d %f\u90fd\u662f\u683c\u5f0f\u66ff\u4ee3\u7b26<\/p>\n\n\n\n

%-10s \u6307\u4e00\u4e2a\u5bbd\u5ea6\u4e3a10\u4e2a\u5b57\u7b26\uff08-\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u6ca1\u6709\u5219\u8868\u793a\u53f3\u5bf9\u9f50\uff09\uff0c\u4efb\u4f55\u5b57\u7b26\u90fd\u4f1a\u88ab\u663e\u793a\u572810\u4e2a\u5b57\u7b26\u5bbd\u7684\u5b57\u7b26\u5185\uff0c\u5982\u679c\u4e0d\u8db3\u5219\u81ea\u52a8\u4ee5\u7a7a\u683c\u586b\u5145\uff0c\u8d85\u8fc7\u4e5f\u4f1a\u5c06\u5185\u5bb9\u5168\u90e8\u663e\u793a\u51fa\u6765\u3002<\/p>\n\n\n\n

%-4.2f \u6307\u683c\u5f0f\u5316\u4e3a\u5c0f\u6570\uff0c\u5176\u4e2d.2\u6307\u4fdd\u75592\u4f4d\u5c0f\u6570\u3002<\/p>\n\n\n\n

\u683c\u5f0f\u63a7\u5236\u5b57\u7b26\u4e32\u4e3a\u53cc\u5f15\u53f7\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# printf \"%d %s\\n\" 1 \"abc\" <\/pre>\n\n\n\n

\u683c\u5f0f\u63a7\u5236\u5b57\u7b26\u4e32\u4e3a\u5355\u5f15\u53f7\uff1a <\/p>\n\n\n\n

[root@linuxcool ~]# printf '%d %s\\n' 1 \"abc\"  <\/pre>\n\n\n\n

\u6ca1\u6709\u5f15\u53f7\u4e5f\u53ef\u4ee5\u8f93\u51fa\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# printf %s abcdef <\/pre>\n","protected":false},"excerpt":{"rendered":"

printf \u547d\u4ee4\u6a21\u4eff C \u7a0b\u5e8f\u5e93\uff08library\uff09\u91cc\u7684 printf() \u7a0b\u5e8f\u3002 printf \u4f7f\u7528\u5f15\u7528\u6587\u672c […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/4020"}],"collection":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/comments?post=4020"}],"version-history":[{"count":2,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/4020\/revisions"}],"predecessor-version":[{"id":4046,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/4020\/revisions\/4046"}],"wp:attachment":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/media?parent=4020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/categories?post=4020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/tags?post=4020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}