Printf (Unix)

Last updated
printf
Developer(s) Various open-source and commercial developers
Operating system Unix and Unix-like
Platform Cross-platform
Type Command
License coreutils: GPLv3+ [1]

In Unix and Unix-like operating systems, printf ("print formatted") is a shell builtin (and utility program [2] ) that formats and prints data.

Contents

The command accepts a printf format string, which specifies methods for formatting items, and a list of items to be formatted. Named historically after the intention of printing to a printer, it now actually outputs to stdout. [3] Characters in the format string are copied to the output or, if a % is encountered, are used to format an item. In addition to the standard formats, %b causes printf to expand backslash escape sequences (for example \n for newline), and %q outputs an item that can be used as shell input. [3] The format string is reused if there are more items than format specs. Unused format specs provide a zero value or null string.

History

printf is part of the X/Open Portability Guide since issue 4 of 1992. It was inherited into the first version of POSIX.1 and the Single Unix Specification. [4] It first appeared in 4.3BSD-Reno. [5]

The version of printf bundled in GNU coreutils was written by David MacKenzie. It has an extension %q for escaping strings in POSIX-shell format. [3]

Examples

$ forNUMBERin468910> do printf " >> %03d %d<< \n" $NUMBER $RANDOM> done >> 004 26305<< >> 006 6687<< >> 008 20170<< >> 009 28322<< >> 010 4400<<

This will print a directory listing, emulating 'ls':

printf "%s\n" * 

See also

Related Research Articles

ls Command to list files and directories in Unix and Unix-like operating systems

In computing, ls is a command to list computer files and directories in Unix and Unix-like operating systems. It is specified by POSIX and the Single UNIX Specification.

pwd Directory information command on various operating systems

In Unix-like and some other operating systems, the pwd command writes the full pathname of the current working directory to the standard output.

basename is a standard computer program on Unix and Unix-like operating systems. When basename is given a pathname, it will delete any prefix up to the last slash ('/') character and return the result. basename is described in the Single UNIX Specification and is primarily used in shell scripts.

uname Standard UNIX utility that prints name and other details about the machine

uname is a computer program in Unix and Unix-like computer operating systems that prints the name, version and other details about the current machine and the operating system running on it.

The printf family of functions in the C programming language are a set of functions that take a format string as input among a variable sized list of other values and produce as output a string that corresponds to the format specifier and given input values. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data to characters. The design has been copied to expose similar functionality in other programming languages.

join is a command in Unix and Unix-like operating systems that merges the lines of two sorted text files based on the presence of a common field. It is similar to the join operator used in relational databases but operating on text files.

tr (Unix) Unix text formatting utility

tr is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.

wc (Unix) Unix command utility

wc is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.

pax is an archiving utility available for various operating systems and defined since 1995. Rather than sort out the incompatible options that have crept up between tar and cpio, along with their implementations across various versions of Unix, the IEEE designed new archive utility pax that could support various archive formats with useful options from both archivers. The pax command is available on Unix and Unix-like operating systems and on IBM i, and Microsoft Windows NT until Windows 2000.

cksum Unix command

cksum is a command in Unix and Unix-like operating systems that generates a checksum value for a file or stream of data. The cksum command reads each file given in its arguments, or standard input if no arguments are provided, and outputs the file's 32-bit cyclic redundancy check (CRC) checksum and byte count. The CRC output by cksum is different from the CRC-32 used in zip, PNG and zlib.

df (Unix) Standard Unix command

df is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate read access. df is typically implemented using the statfs or statvfs system calls.

nl is a Unix utility for numbering lines, either from a file or from standard input, reproducing output on standard output.

cmp (Unix)

In computing, cmp is a command-line utility on Unix and Unix-like operating systems that compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported. The command is also available in the OS-9 shell.

who (Unix)

The standard Unix command who displays a list of users who are currently logged into the computer.

env is a shell command for Unix and Unix-like operating systems. It is used to either print a list of environment variables or run another utility in an altered environment without having to modify the currently existing environment. Using env, variables may be added or removed, and existing variables may be changed by assigning new values to them.

sum is a legacy utility available on some Unix and Unix-like operating systems. This utility outputs a 16-bit checksum of each argument file, as well as the number of blocks they take on disk. Two different checksum algorithms are in use. POSIX abandoned sum in favor of cksum.

Getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems. It is also the name of a Unix program for parsing command line arguments in shell scripts.

The csplit command in Unix and Unix-like operating systems is a utility that is used to split a file into two or more smaller files determined by context lines.

fold is a Unix command used for making a file with long lines more readable on a limited width computer terminal by performing a line wrap.

cat (Unix) Unix command utility

cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to (con)catenate files. It has been ported to a number of operating systems.

References

  1. "printf(1): format/print data - Linux man page". linux.die.net.
  2. "GNU Coreutils". www.gnu.org.
  3. 1 2 3 printf(1)    Linux User Manual – User Commands
  4. printf   Shell and Utilities Reference, The Single UNIX Specification , Version 4 from The Open Group
  5. printf(1)    FreeBSD General Commands Manual