Shell printf command


In the previous chapter, we learned about the echo command of Shell. In this chapter, we will learn about printf, another output command of Shell.

The printf command imitates the printf() program in the C library.

is defined by the standard, so scripts using printf are more portable than using echo.

printf uses quoted text or space-delimited parameters. You can use formatting strings in printf, and you can also specify the width, left and right alignment of the string, etc. By default printf does not automatically add newlines like echo, we can add \n manually.

Printf command syntax:

printf format-string [arguments...]

Parameter description:

  • format-string:is Format control string

  • arguments:is the parameter list.

The example is as follows:

$ echo "Hello, Shell" Hello, Shell $ printf "Hello, Shell\n" Hello, Shell $

Next, I will use a script to reflect the powerful function of printf:

#!/bin/bash # author:php中文网 # url:m.sbmmt.com printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876

Execute the script, the output result is as follows Shown:

姓名 性别 体重kg 郭靖 男 66.12 杨过 男 48.65 郭芙 女 47.99

%s %c %d %f are all format substitutes

%-10s refers to a width of 10 characters (- means left alignment, no means right alignment ), any characters will be displayed within 10 characters wide. If it is insufficient, it will be automatically filled with spaces. If it is exceeded, all the content will be displayed.

%-4.2f refers to formatting as a decimal, where .2 refers to retaining 2 decimal places.


More examples:

#!/bin/bash # author:php中文网 # url:m.sbmmt.com # format-string为双引号 printf "%d %s\n" 1 "abc" # 单引号与双引号效果一样 printf '%d %s\n' 1 "abc" # 没有引号也可以输出 printf %s abcdef # 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用 printf %s abc def printf "%s\n" abc def printf "%s %s %s\n" a b c d e f g h i j # 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替 printf "%s and %d \n"

Execute the script, the output result is as follows:

1 abc 1 abc abcdefabcdefabc def a b c d e f g h i j and 0

The escape sequence of printf

##\n Line feed \r Carriage return \t Horizontal tab character \v Vertical tab character \\ A literal backslash character \ddd A character representing a 1 to 3 digit octal value. Valid only in format strings \0ddd Represents an octal value character of 1 to 3 digits
Sequence Description
\a Warning character, usually ASCII BEL Characters
\b Back
\c Suppresses (does not display) any endings in the output result newline character (valid only in parameter strings controlled by the %b format specifier), and any characters left in the parameter, any subsequent parameters, and any characters left in the format string are ignored
\f Formfeed
Example

$ printf "a string, no processing:<%s>\n" "A\nB" a string, no processing: $ printf "a string, no processing:<%b>\n" "A\nB" a string, no processing: $ printf "m.sbmmt.com \a" m.sbmmt.com $ #不换行