TrumanWong

printf

Format and output the results.

Table of contents

  • [bash built-in command](#built-in command)
  • [Commands in GNU coreutils](#External commands)

Built-in commands

Summary

printf [-v var] format [arguments]

The main purpose

  • Format parameters and output.

Options

-v var: Output the results to the variable var instead of outputting to standard output.

Parameters

format: output format.

arguments: one to multiple parameters.

Escape sequences: In addition to supporting the escape sequences of printf(1) and printf(3), the built-in printf also supports the following escape sequences:

%b expands backslash-escaped characters in arguments.
%q expands arguments for use as shell input.
%(fmt)T Outputs a date and time string based on the escape characters in strftime(3).

return value

The return status is success unless an illegal option, write error, or assignment error is given.

example

# %-5s is replaced by a left-aligned string with a width of 5 ('-' means left-aligned). If not used, it defaults to right-aligned.
# %-4.2f format is left-aligned with a width of 4 and two decimal places.

printf "%-5s %-10s %-4s\n" NO Name Mark
printf "%-5s %-10s %-4.2f\n" 01 Tom 90.3456
printf "%-5s %-10s %-4.2f\n" 02 Jack 89.2345
printf "%-5s %-10s %-4.2f\n" 03 Jeff 98.4323

# output
NO Name Mark
01 Tom 90.35
02 Jack 89.23
03 Jeff 98.43


# Example of %b %q %(fmt)T.
# see it again with a newline.
printf "%s\n" 'hello world'
# Expand newlines, same result as above.
printf "%b" 'hello world\n'

printf '%q\n' 'a b c'
# output
a\ b\ c

# %z is the time zone, %n is the newline character.
printf "%(%F %T %z%n)T"
# output
2019-09-10 01:48:07 +0000

Notice

  1. This command is a built-in bash command. For related help information, please see the help command.

External commands

Summary

printf FORMAT [ARGUMENT]...
printf OPTION

The main purpose

  • Format parameters and output.

Options

--help Display help information and exit.
--version Display version information and exit.

Parameters

format: output format.

arguments: one to multiple parameters.

(%b %q) are ignored here, if the version of coreutils you installed supports them, then please refer to the example above.
Supported escape sequences:

\"          Double quotes
\\ backslash
\a ring
\b backspace
\c truncate output
\e exit
\f turn page
\n newline
\r Enter
\t horizontal tab character
\v vertical tab character
\NNN Octal number (1 to 3 digits)
\xHH hexadecimal number (1 to 2 digits)
\uHHHH Unicode character appends 4 hexadecimal digits
\UHHHHHHHH Unicode character appends 8 hexadecimal digits
%% percent sign

and a trailing C format specification in 'diouxXfeEgGcs' that will be converted to the correct type and handle variable widths.

example

# Use /usr/bin/printf to ensure that the built-in command is not called.
# Of course, if you turn off the built-in printf and confirm that the current environment does not have a printf function, you can use printf directly. For details, see the "Note" link at the end.

# Print the subscripts and values of arrays and associative arrays line by line.

# You can declare an array without adding 'declare -a' or 'local -a' (local variables declared within a function).
arr=('line1' 'line2')
/usr/bin/printf "%s\n" ${!arr[@]}
# Output subscript
0
1
/usr/bin/printf "%s\n" ${arr[@]}
# output value
line1
line2

#Declaring an associative array (that is, a dictionary) must add 'declare -A' or 'local -A' (local variables declared within the function).
declare -A assoc_arr=(['key1']='value1' ['key2']='value2')
/usr/bin/printf "%s\n" ${!assoc_arr[@]}
# Output key.
key2
key1
/usr/bin/printf "%s\n" ${assoc_arr[@]}
# output value.
value2
value1

return value

The return status is success unless illegal options are given, etc.

Notice

  1. This command is a command in the GNU coreutils package. For related help information, please see man -s 1 printf or info coreutils 'pwd invocation'.

  2. To enable or disable built-in commands, please see the enable command. For issues with the priority of the same name, please see the relevant discussion in the examples section of the builtin command.

  3. Through communication with bug-bash@gnu.org, I got the explanation of these format specifiers %b %q %(fmt)T:

    The %b format specifier in printf(1) is a POSIX feature added to the formats supported by printf(3).

    The %q and %T specifiers are non-standard and not supported by all independent implementations of printf.

    For more details please refer to the link: