printf (将参数插入用户定义的文本字符串中,从而创建格式化的输出)

瑞兹 发表于 2020-11-15 02:43
浏览次数:
在手机上阅读

在类似Unix的操作系统上,printf命令将参数插入用户定义的文本字符串中,从而创建格式化的输出。

查看英文版

目录

1 printf 运行系统环境

2 printf 描述

3 printf 语法

4 printf 例子

printf 运行系统环境

Linux

printf 描述

printf格式化的字符串打印到标准输出。它的根源是C编程语言,它使用具有相同名称的函数。这是从数字或文本参数产生精确格式输出的一种便捷方法。
printf prints a formatted string to the standard output. Its roots are in the C programming language, which uses a function by the same name. It is a handy way to produce precisely-formatted output from numerical or textual arguments.

查看英文版

查看中文版

printf 语法

printf FORMAT [ARGUMENT]...
printf OPTION

选件

FORMAT FORMAT控制输出,并定义将在输出中表达ARGUMENT的方式。请参阅下面的“FORMA”部分。
ARGUMENT 根据FORMAT的定义,每个ARGUMENT都将插入到格式化输出中。
- help 显示帮助消息,然后退出。
- version 显示版本信息,然后退出。

格式

格式字符串包含三种类型的对象:

  • 普通字符,将原样复制到输出中。
  • 解释的字符序列,使用反斜杠(“ \ ”)进行转义。
  • 转换规范,用于定义将ARGUMENT表示为输出一部分的方式。 

这是一个使用这三种对象的快速示例:

" printf" My name is\"%s \".\ nIt's a pleasure to meet you." "John"

此命令产生输出:

My name is "John". 
It's a pleasure to meet you.

在这里,FORMAT用双引号()括起来。有一个转换规范:%s,它将参数” John“解释为字符串并将其插入输出中。有三个转义的字符序列:两次出现\ “\ n出现一次。序列\“转换为文字双引号;以反斜杠转义,以便printf知道将其视为文字字符而不是FORMAT字符串的结尾。\ n是换行符的序列,并告诉printf 开始新的一行并从那里继续输出。

的功率的printf就在于,对于任何给定的格式字符串,则ARGUMENT s时,可以改变影响输出。例如,可以仅通过更改参数“ John”来更改以上示例中命令的输出。如果在脚本中使用,则此参数可以设置为变量。例如,命令

printf "Hi, I'm %s. \s. \n" $ LOGNAME

...将插入环境变量 $ LOGNAME的值,该值是运行命令的人的用户名。

转换规格

每个转换规范都以开头,以转换字符结尾。在和转换字符之间可能有以下顺序:

- 减号。这告诉printf向左调整参数的转换。
number 一个整数,它指定字段的宽度;printf将在至少数字字符宽的字段中打印ARGUMENT的转换。如有必要,将在左侧填充(或在需要进行左调整的情况下在右侧填充)以填充字段宽度。
. 一个周期,它将字段宽度与精度分开。
number 整数,精度,它指定要从字符串中打印的最大字符数,或者浮点值的小数点后的位数,或者整数的最小位数。
or l 它们分别区分短整数和长整数,通常仅在计算机编程时才需要。

转换字符本身(告诉printf需要什么样的参数)如下:

转换字符 参数类型
di 整数,表示为十进制数。
o 整数,表示为无符号八进制数。
xX 整数,表示为无符号十六进制数
u 整数,表示为无符号十进制数。
c 整数,表示为字符。整数对应于字符的ASCII码。
s 一个字符串。
F 浮点数,默认精度为6。
eE 以科学计数法表示的浮点数,默认精度为6。
p 内存地址指针。
没有转换;而是打印文字百分号(“  ”)。

宽度或精度可以用星号(“ * ”)表示;如果是这样,星号将读取一个必须为整数的参数,并使用该值。例如,

printf "%.* s" 5 "abcdefg"

...产生以下输出:

abcde

下表说明了printf使用各种FORMAT字符串输出其ARGUMENT(“ computerhope ”)的方式。每个字符串都用引号引起来,以便更轻松地查看每个字符串的确切范围:

格式字符串 参数字符串 输出字符串
“%s” “computerhope”
“computerhope”
“%8s” “computerhope”
computerhope
“%.8s” “computerhope”
“computer”
“%-8s” “computerhope”
“computerhope”
“%-15s” “computerhope”
“computerhope”
“%15.8s” “computerhope”
“computer”
“%-15.8” “computerhope”
“computer”
“%-15.2” “computerhope
“ co”

请注意,printf要求转换字符串的数量与ARGUMENT的数量相匹配。它一对一地映射它们,并期望为每个转换字符串找到一个准确的ARGUMENT。唯一的例外是使用星号的转换字符串。这样的字符串每个都需要两个参数。

转换字符串始终从左向右解释。例如,以下printf命令:

“ printf”%d puls %5f%s%。* f。5 5.05“equals” 3 10.05

...产生以下输出:

5 plus 5.050000 equals 10.050。

解释的转义字符序列

以下字符序列由printf解释为特殊字符:

\“ 打印双引号(
\\ 打印反斜杠(\
\a 发出警报(按铃)
\b 打印退格键
\c 指示printf不产生进一步的输出
\e 打印转义字符(ASCII代码27)
\f 打印换页
\n 打印换行符
\r 打印回车
\t 打印水平标签
\v 打印垂直标签
\NNN 打印八进制值NNN(1到3位数字)的字节
\xHH 打印一个十六进制值HH(1到2位数字)的字节
\uHHHH 打印具有十六进制值HHHH(4位)的Unicode字符
\UHHHHHHHH 打印具有十六进制值HHHHHHHH(8位)的Unicode字符
%b ARGUMENT打印为带有“ \ ”转义符的字符串,其解释如上所列,但八进制转义符的格式为\ 0\ 0 NN

壳牌行情

注意您的外壳解释带引号的字符串的方式。如果您的外壳无法正确解释带引号的字符串,请尝试使用单引号而不是双引号。

printf FORMAT [ARGUMENT]...
printf OPTION

Options

FORMAT FORMAT controls the output, and defines the way that the ARGUMENTs will be expressed in the output. See the Format section, below.
ARGUMENT Each ARGUMENT will be inserted into the formatted output according to the definition of FORMAT.
--help Display a help message, and exit.
--version Display version information, and exit.

Format

The FORMAT string contains three types of objects:

  • ordinary characters, which are copied verbatim to the output. 
  • interpreted character sequences, which are escaped with a backslash ("\").  
  • conversion specifications, which define the way in which ARGUMENTs will be expressed as part of the output. 

Here is a quick example which uses these three types of objects:

printf "My name is \"%s\".\nIt's a pleasure to meet you." "John"

This command produces the output:

My name is "John".
It's a pleasure to meet you.

Here, FORMAT is enclosed in double-quotes ("). There is one conversion specification: %s, which interprets the argument "John" as a string and inserts it into the output. There are three escaped character sequences: two occurrences of \" and one occurrence of \n. The sequence \" translates as a literal double-quote; it is escaped with a backslash so that printf knows to treat it as a literal character, and not as the end of the FORMAT string. \n is the sequence for a newline character, and tells printf to begin a new line and continue the output from there.

The power of printf lies in the fact that for any given FORMAT string, the ARGUMENTs can be changed to affect the output. For example, the output of the command in the above example can be altered just by changing the argument, "John". If used in a script, this argument can be set to a variable. For instance, the command

printf "Hi, I'm %s.\n" $LOGNAME

...will insert the value of the environment variable $LOGNAME, which is the username of whoever ran the command.

Conversion Specifications

Each conversion specification begins with a % and ends with a conversion character. Between the % and the conversion character there may be, in order:

- A minus sign. This tells printf to left-adjust the conversion of the argument.
number An integer that specifies field width; printf will print a conversion of ARGUMENT in a field at least number characters wide. If necessary it will be padded on the left (or right, if left-adjustment is called for) to make up the field width.
. A period, which separates the field width from the precision.
number An integer, the precision, which specifies the maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value, or the minimum number of digits for an integer.
h or l These differentiate between a short and a long integer, respectively, and are generally only needed for computer programming.

The conversion characters themselves, which tell printf what kind of argument to expect, are as follows:

conversion character argument type
di An integer, expressed as a decimal number.
o An integer, expressed as an unsigned octal number.
xX An integer, expressed as an unsigned hexadecimal number
u An integer, expressed as an unsigned decimal number.
c An integer, expressed as a character. The integer corresponds to the character's ASCII code.
s A string.
f A floating-point number, with a default precision of 6.
eE A floating-point number expressed in scientific notation, with a default precision of 6.
p A memory address pointer.
% No conversion; a literal percent sign ("%") is printed instead.

A width or precision may be represented with an asterisk ("*"); if so, the asterisk reads in an argument, which must be an integer, and uses that value. For example,

printf "%.*s" 5 "abcdefg"

...produces the following output:

abcde

The following table represents the way that printf would output its ARGUMENT, "computerhope", using various FORMAT strings. Each string is enclosed in quotes so that it's easier to see the exact extent of each:

FORMAT string ARGUMENT string output string
"%s" "computerhope"
"computerhope"
"%8s" "computerhope"
"computerhope"
"%.8s" "computerhope"
"computer"
"%-8s" "computerhope"
"computerhope"
"%-15s" "computerhope"
"computerhope"
"%15.8s" "computerhope"
"computer"
"%-15.8" "computerhope"
"computer"
"%-15.2" "computerhope"
"co "

Please note that printf requires the number of conversion strings to match the number of ARGUMENTs; it maps them one-to-one, and expects to find exactly one ARGUMENT for each conversion string. The only exception is a conversion string which uses an asterisk; such strings require two arguments each.

Conversion strings are always interpreted from left to right. For example, the following printf command:

printf "%d plus %5f %s %.*f." 5 5.05 "equals" 3 10.05

...produces the following output:

5 plus 5.050000 equals 10.050.

Interpreted Escaped Character Sequences

The following character sequences are interpreted as special characters by printf:

\" prints a double-quote (")
\\ prints a backslash (\)
\a issues an alert (plays a bell)
\b prints a backspace
\c instructs printf to produce no further output
\e prints an escape character (ASCII code 27)
\f prints a form feed
\n prints a newline
\r prints a carriage return
\t prints a horizontal tab
\v prints a vertical tab
\NNN prints a byte with octal value NNN (1 to 3 digits)
\xHH prints a byte with hexadecimal value HH (1 to 2 digits)
\uHHHH prints the unicode character with hexadecimal value HHHH (4 digits)
\UHHHHHHHH prints the unicode character with hexadecimal value HHHHHHHH (8 digits)
%b prints ARGUMENT as a string with "\" escapes interpreted as listed above, with the exception that octal escapes take the form \0 or \0NN

Quoting In The Shell

Be careful with the way your shell interprets quoted strings. If your shell is not interpreting your quoted string correctly, try using single-quotes rather than double-quotes.

查看英文版

查看中文版

printf 例子

printf 'hello\nworld\n!'

打印以下输出:

hello 
world 
!
printf "%b" 'hello\nworld\n!'

打印与上述示例相同的输出。

printf "Your home folder is %s.\n" $HOME

打印一个字符串,告诉您主目录的位置。

printf 'hello\nworld\n!'

Prints the following output:

hello 
world 
!
printf "%b" 'hello\nworld\n!'

Prints the same output as the above example.

printf "Your home folder is %s.\n" $HOME

Prints a string telling you the location of your home directory.

查看英文版

查看中文版

其他命令行

pack | pagesize | parted | partprobe | paste | passwd | pax | pcat | pg | perl | pgrep | pico | pine | ping | pr | printenv | priocntl | pstree | pvs | pwd |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条