国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > printf 以获得更好的体验。

# Bash 的 printf 命令

printf [OPTION] FORMAT [ARGUMENT]...

!subtitle:功能

数值和字符串之间的转换。

!subtitle:类型

可执行文件(/usr/bin/printf),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • --help - 显示帮助

    • --version - 显示版本

  • FORMAT - 格式字符串

  • ARGUMENT - 参数列表

# 格式

符号 说明
\" 双引号
\\ 反斜杠
\a 警告声(BEL)
\b 退格
\c 不再产生任何输出
\e Escape 字符
\f 换页符(form feed)
\n 换行符
\r 回车符
\t 水平制表符
\v 垂直制表符
\NNN 八进制值为 NNN 的字节(1 到 3 位)
\xHH 十六进制值为 HH 的字节(1 到 2 位)
\uHHHH Unicode 字符(ISO/IEC 10646),十六进制值 HHHH(4 位)
\UHHHHHHHH Unicode 字符,十六进制值 HHHHHHHH(8 位)

| %% | 百分号(%)本身
| %b | 将参数作为带有反斜杠转义的字符串输出;八进制转义格式为 \0 或 \0NNN | | %q | 输出可作为 shell 输入的格式,并使用 POSIX 提议的 $'' 语法对不可打印字符进行转义 | | %c | 单个字符 | | %s | 字符串 | | %d | 有符号十进制整数 | | %i | 有符号十进制整数,同 %d | | %u | 无符号十进制整数 | | %o | 无符号八进制整数 | | %x | 无符号十六进制整数,字母小写 | | %X | 无符号十六进制整数,字母大写 | | %f | 浮点数,字母小写 | | %F | 浮点数,同 %f | | %e | 浮点数,科学计数法,字母小写 | | %E | 浮点数,科学计数法,字母大写 | | %g | 浮点数,省略小数末尾的 0,值较大时使用科学计数法,字母小写 | | %G | 浮点数,省略小数末尾的 0,值较大时使用科学计数法,字母大写 |

# 示例

$ printf "Hello World\n"
Hello World
$ printf "Name: %s, Age: %d\n" "Alice" 18
Name: Alice, Age: 18

# 推荐阅读

# 手册

PRINTF(1)                        User Commands                       PRINTF(1)

NAME
       printf - format and print data

SYNOPSIS
       printf FORMAT [ARGUMENT]...
       printf OPTION

DESCRIPTION
       Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:

       --help display this help and exit

       --version
              output version information and exit

       FORMAT controls the output as in C printf.  Interpreted sequences are:

       \"     double quote

       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     produce no further output

       \e     escape

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab

       \NNN   byte with octal value NNN (1 to 3 digits)

       \xHH   byte with hexadecimal value HH (1 to 2 digits)

       \uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)

       \UHHHHHHHH
              Unicode character with hex value HHHHHHHH (8 digits)

       %%     a single %

       %b     ARGUMENT  as  a string with '\' escapes interpreted, except that
              octal escapes are of the form \0 or \0NNN

       %q     ARGUMENT is printed in a format that can be reused as shell  in‐
              put,  escaping  non-printable characters with the proposed POSIX
              $'' syntax.

       and all C format specifications ending with one of diouxXfeEgGcs,  with
       ARGUMENTs converted to proper type first.  Variable widths are handled.

       NOTE:  your shell may have its own version of printf, which usually su‐
       persedes the version described here.  Please refer to your shell's doc‐
       umentation for details about the options it supports.

AUTHOR
       Written by David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       printf(3)

       Full documentation <https://www.gnu.org/software/coreutils/printf>
       or available locally via: info '(coreutils) printf invocation'

GNU coreutils 9.4                 April 2024                         PRINTF(1)
本文 更新于: 2025-11-27 09:38:15 创建于: 2025-11-27 09:38:15