Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

printf - formatted output

&pagelevel(4)&pagelevel

The printf command outputs the arguments you specify in formatted form. printf supports all format specifications for strings as in the printf() function in C.


Syntax


printf  format[ arg]...

format

Character string that can contain three different types of objects:

  • plain characters, which are output without any modifications.

  • Escape sequences for metacharacters, which are converted into the corresponding characters in the output, e.g. \n is converted to a newline character.

  • Format elements from which each one of the specified arguments arg is processed.

arg

String to be written to standard output in the format specified by format. If there are fewer arguments than expected by format, the missing arguments are set to 0 or the null string. If there are more arguments than expected by format, format is applied more than once (unless arg_no$ is specified, in which case the excess arguments are ignored).

arg not specified:
The result is undefined.        

Metacharacters

The following metacharacters are interpreted by printf:

\\ Backslash (for distinguishing octal characters)
\a Warning, alert *)
\b Backspace *)
\f Form Feed
\n Newline
\r Carriage Return
\t Tab
\v Vertical tab *)
\ octalOctal number, whereby octal consists of one, two, or three digits

*)   These metacharacters are supported only on character terminals (i.e. if you are accessing the POSIX shell via rlogin)

Format elements

A format element comprises:

%[arg_no$][field_width][.precision]conversion_character


%

Always located at the beginning of the format element. If the % character is not to be interpreted as a part of the format element but as an ordinary character to be output, it must be escaped by preceding it with another % (%%).

arg_no$

Decimal integer with which you specify the position of the argument to be processed. The number must be followed by a $ character.

%arg_nr$ and % should not be used in combination.

arg_no not specified:
The argument following the last converted argument is used.


If you use arg_nr$ for one argument, you should also use arg_nr$ for all the other arguments.


field_width

Decimal integer with which you specify the minimum field width. If the string to be converted has fewer characters than field_width, it is padded on the left to the field width and output right-adjusted. If left-adjustment is desired, the decimal integer must be preceded by a dash (-). The padding is with blanks unless the field_width integer starts with a zero, in which case padding for right-adjusted output is done with zeros.
A field_width may also be indicated by an asterisk (*) instead of an integer. In this case, an integer argument supplies the field width. This argument must appear before the string to be converted. The asterisk does not work in combination with arg_no$.
If the string is longer than the field width, the field is automatically expanded.

.precision

Decimal integer with which you specify the maximum number of characters to be output from the string to be converted. This number must be preceded by a dot (.). If the precision argument is zero, nothing is output. The number of characters output is always controlled by the precision, even if some other value has been specified for the field width.
A precision may also be indicated by an asterisk (*) instead of an integer. In this case, an integer argument supplies the precision. This argument must appear before the string to be converted. The asterisk does not work in combination with arg_no$.

conversion_character

The following conversion_characters can be used for printf:


b character string with metacharacters
c single character
d signed decimal integer
e floating-point number in exponential notation, e.g. 5.234e+2
E floating-point number in exponential notation, e.g. 5.234e+2
f floating-point number, e.g. 52.34
g %e or %f, whichever is shorter
G %E or %f, whichever is shorter
o signed octal integer (base 8)
s character string
u unsigned decimal integer
x unsigned hexadecimal integer (base 16)


With s all characters from the character string are output until the number of characters specified for precision is reached. If precision is not specified, the entire character string is output.

With b, the character string in arg can contain metacharacters. printf supports all escape sequences interpreted by the echo command in this case, i.e. the escape sequences specified above with the exception of octal numbers specified as \0octal, and \c. \c causes printf to abort output at this point and not to terminate it with a newline character.

Locale

The following environment variables affect the execution of printf:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

LC_NUMERIC

Determine the locale for numeric formatting. It will affect the format of numbers written using the e, E, f, g and G conversion characters.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

Output the string "Good Morning Helen" on the screen:

$ printf '%s %s %s\n' Good Morning Helen

The following command produces the same output:

$ printf '%2$s %3s %1$s\n' Helen Good Morning

Example 2

Output the first 6 characters of your home directory /usr/kathy with an appropriate message:

$ printf 'The first 6 characters of %s are %.6s.\n' $HOME $HOME

The first 6 characters of /usr/kathy are /usr/k.

Example 3

The examples that follow write a 4-digit number in an 8-character field right-adjusted, right-adjusted with leading zeros, and left-adjusted, respectively:

$ printf '%8s\n' 1860
    1860
$ printf '%08s\n' 1860
00001860
$ printf '%-8s\n' 1860
1860'    '

See also

awk, bc, echo
printf() [4]