printf

printf #

按格式打印参数。

text
printf FORMAT [ARGUMENT]...

    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 input,  escaping  non-printable  characters
            with the proposed POSIX $'' syntax.

样例 #

sh
# 输出指定格式的内容
printf 'name=%s\nvalue=%s\n' name1 value1
# Output:
# name=name1
# value=value1

# 输出可在 shell 中作为参数使用的内容
printf '%q' 'Hello world'
# Output: Hello\ world

# 输出10个等号
printf '=%.0s' {1..10}
# Output: ==========
2025年8月10日