printf

display a formatted string 

Command


SYNOPSIS

printf format [argument ...]


DESCRIPTION

printf writes the argument operand(s) to standard output, formatted according to the format operand.

format is a format string composed of conversion specifications that convert and add the next argument to the output. format can contain backslash-escape sequences. The conversions are similar to those used by the printf() function in the ANSI C standard. Conversion specifications have the form:

%[flag][width][.precision][[mod]char]

where flag is one of the following:

- 

left justifies the field; default is right justification.

+ 

always prefixes a signed value with a sign (+ or -).

space 

reserves a character position at the start of the string for the minus sign (for negative numbers) or a space (for positive numbers). If both space and - appear as flags, the space flag is ignored.

# 

prefixes octal values with 0 and hexadecimal values with 0x or 0X. For floating point values, this forces the decimal point to be displayed, even if no characters follow it.

0 

pads numeric values with leading zeros. If both 0 and - appear as flags, the 0 flag is ignored.

' 

When the environment variable TK_USE_CURRENT_LOCALE is set, a thousands separator is displayed for decimal integers and floating point conversions. The digital grouping character (for example, a comma in the United States) as set by the Regional and Language Options control panel applet is used as the thousands separator. When TK_USE_CURRENT_LOCALE is unset, no thousands separator is displayed.

For example, the following MKS KornShell commands:

export TK_USE_CURRENT_LOCALE=1
printf "%'10d\n" 1234567

display:

1,234,567

while the MKS KornShell commands:

unset TK_USE_CURRENT_LOCALE
printf "%'10d\n" 1234567

display:

1234567
" 

is the same as '.

width is the minimum field width of the output field. If the converted value is shorter than the minimum width, printf pads it with spaces or zeros.

In a string, precision is the maximum number of bytes to be printed from the string; in a number, the precision is the number of digits to be printed to the right of the decimal point in a floating point value. width or precision may be specified as *, in which case the value is read from the next argument, which must be an integer. For example:

printf "%*.*d\n" 20 10 200

is equivalent to

printf "%20.10d\n" 200

The optional conversion modifier mod modifies the conversion character that follows. The following are acceptable modifiers:

l 

specifies that a following d, i, o, u, x, or X conversion specifier applies to a 32-bit argument.

ll 
I64 

specifies that a following d, i, o, u, x, or X conversion specifier applies to a 64-bit argument.

The conversion character char is one of the following:

b 

a string which may contain a backslash-escape sequence. Valid escape sequences are those described on the echo reference page, \0ddd where ddd is a zero- to three-digit octal number, \xdd where dd is a zero- to two digit hexadecimal number, and \c which causes printf to ignore the remainder of that argument, any other arguments and the remainder of the format string.

c 

the first character of a string; number arguments are treated as strings.

d 

decimal integer. The value must be between -2147483648 and 2147483647 for 32-bit integers and -9223372036854775808 and 9223372036854775807 for 64-bit integers.

E,e 

floating point (scientific notation).

f 

floating point.

G,g 

the shorter of e and f (suppresses non-significant zeros).

i 

decimal integer.

o 

unsigned octal integer.

q 

special characters in argument are quoted.

s 

string.

u 

unsigned decimal integer. The value must be between 0 and 4294967295 for 32-bit integers and between 0 and 18446744073709551615 for 64-bit integers.

X,x 

unsigned hexadecimal integer.

When there are more arguments than positions in format, the format string is applied again to the remaining arguments. When there are fewer arguments than positions in the format string, printf fills the remaining positions with null-strings (character fields) or zeros (numeric fields).


ENVIRONMENT VARIABLES

TK_USE_CURRENT_LOCALE 

specifies whether or not to use relevant information from the current locale. When set, current locale information is used; when unset, the default locale information is used.

Note:

Current locale information is set using the Regional and Language Options control panel applet.

For printf, this environment variable determines the characters displayed for the decimal point and the thousands separator.


DIAGNOSTICS

Possible exit status values are:

0 

Successful completion.

>0 

The number of failures due to any of the following:

— missing format specifications
arguments supplied for a format string which does not accept them (that is, has no %)
— invalid integer argument
— invalid floating point argument


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. UNIX System V. Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022.

The handling of * as a width or precision argument is an extension to the POSIX standard. The \xnn backslash escape for hexadecimal is an extension to the POSIX standard.


WARNING

The POSIX.2 printf facility (like the C language printf() on which it is based) does not gracefully accommodate multibyte characters when using %c conversion, or with either %b or %s conversions with a specified precision. Use these features cautiously when you have multibyte characters in the character set.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Commands:
echo, print


PTC MKS Toolkit 10.4 Documentation Build 39.