Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

printf (Unix)

From Wikipedia, the free encyclopedia
Shell command for formatting and outputting text; like printf() library function
icon
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
DevelopersVariousopen-source andcommercial developers
Operating systemUnix andUnix-like
PlatformCross-platform
TypeCommand
Licensecoreutils: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]

Examples

[edit]

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"*


References

[edit]
  1. ^"printf(1): format/print data - Linux man page".linux.die.net.Archived from the original on 2018-11-21. Retrieved2019-02-24.
  2. ^"GNU Coreutils".www.gnu.org.Archived from the original on 2004-02-05. Retrieved2019-02-27.
  3. ^abcprintf(1) – Linux UserManual – User Commands from Manned.org
  4. ^printf – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  5. ^printf(1) – FreeBSD General CommandsManual

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
Retrieved from "https://en.wikipedia.org/w/index.php?title=Printf_(Unix)&oldid=1336333130"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp