This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Printf" Unix – news ·newspapers ·books ·scholar ·JSTOR(July 2010) (Learn how and when to remove this message) |
| printf | |
|---|---|
| Developers | Variousopen-source andcommercial developers |
| Operating system | Unix andUnix-like |
| Platform | Cross-platform |
| Type | Command |
| License | coreutils:GPLv3+[1] |
printf is ashellcommand that formats and outputs text like thesame-named C function. It is available in a variety ofUnix andUnix-like systems. Some shells implement the command asbuiltin and some provide it as autilityprogram[2]
The command has similarsyntax andsemantics as the library function. The command outputs text tostandard output[3] as specified by a format string and a list of values.Characters of the format string are copied to the output verbatim except when a format specifier is found which causes a value to be output per the specifier.
The command has some aspects unlike the library function. In addition to the library function format specifiers,%b causes the command to expand backslashescape sequences (for example\n fornewline), and%q outputs an item that can be used asshell input.[3] The value used for an unmatched specifier (too few values) is an empty string for%s or 0 for a numeric specifier. If there are more values than specifiers, then the command restarts processing the format string from its beginning,
The command is part of theX/Open Portability Guide since issue 4 of 1992. It was inherited into the first version of POSIX.1 and theSingle Unix Specification.[4] It first appeared in4.3BSD-Reno.[5]
The implementation bundled inGNU Core Utilities was written by David MacKenzie. It has an extension%q for escaping strings in POSIX-shell format.[3]
This prints a list of numbers:
$forNin4810;doprintf" >> %03d << \n"$N;done>>004<<>>008<<>>010<<
This produces output for a directory's content similar tols:
$printf"%s\n"*
printf(1) – Linux UserManual – User Commands from Manned.orgprintf – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Groupprintf(1) – FreeBSD General CommandsManual