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]...
|
Character string that can contain three different types of objects:
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: |
Metacharacters
The following metacharacters are interpreted by printf:
*) |
Format elements
A format element comprises:
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 % (%%).
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: If you use arg_nr$ for one argument, you should also use arg_nr$ for all the other arguments.
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.
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.
The following conversion_characters can be used for printf:
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:
The following command produces the same output:
|
Example 2
Output the first 6 characters of your home directory /usr/kathy with an appropriate message:
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:
|
See also
awk, bc, echo printf() [4] |