Classic Shell Scripting - Arnold Robbins [19]
printf format-string [arguments ...]
The first part is a string describing the desired output; this is best supplied as a string constant in quotes. This string is a mixture of characters to be printed literally, and format specifications, which are special placeholders that describe how to print each corresponding argument.
The second part is an argument list, such as a list of strings or variable values, that correspond to the format specifications. (If there are more arguments than format specifications, printf cycles through the format specifications in the format string, reusing them in order, until done.) A format specification is preceded by a percent sign (%) and the specifier is one of the characters described later in the book. Two of the main format specifiers are %s for strings and %d for decimal integers.
Within the format string, regular characters are printed verbatim. Escape sequences, similar to those of echo, are interpreted and then output as the corresponding character. Format specifiers, which begin with the character % and end with one of a defined set of letters, control the output of the following corresponding arguments. For example, %s is used for strings:
$ printf "The first program always prints '%s, %s!'\n" Hello world
The first program always prints 'Hello, world!'
All the details on printf are given in Section 7.4.
Basic I/O Redirection
Standard I/O is perhaps the most fundamental concept in the Software Tools philosophy.[6] The idea is that programs should have a data source, a data sink (where data goes), and a place to report problems. These are referred to by the names standard input, standard output, and standard error, respectively. A program should neither know, nor care, what kind of device lies behind its input and outputs: disk files, terminals, tape drives, network connections, or even another running program! A program can expect these standard places to be already open and ready to use when it starts up.
Many, if not most, Unix programs follow this design. By default, they read standard input, write standard output, and send error messages to standard error. Such programs are called filters, for reasons that will become clear shortly. The default for standard input, standard output, and standard error is the terminal. This can be seen with cat:
$ cat
With no arguments, read standard input, write standard output
now is the time
Typed by the user
now is the time Echoed back by cat
for all good men
for all good men
to come to the aid of their country
to come to the aid of their country
^D
Ctrl-D, End of file
You may be wondering, who initializes standard input, output, and error for a running program? After all, somebody has to open these files for any given program, even the interactive shell that each user sees at login!
The answer is that when you log in, Unix arranges the default place for standard input, output, and error to be your terminal. I/O redirection is the process by which you, at the terminal interactively, or from within a shell script, then arrange to change the places from which input comes or to which output goes.
Redirection and pipelines
The shell provides several syntactic notations for specifying how to change the default I/O sources and destinations. We cover the basic ones here; later we'll provide the full story. Moving from simple to complex, these notations are as follows:
Change standard input with <
Use program < file to make program's standard input be file:
tr -d '\r' < dos-file.txt ...
Change standard output with >
Use program > file to make program's standard output be file:
tr -d '\r' < dos-file.txt > unix-file.txt
This tr invocation removes ASCII carriage-return characters from dos-file.txt, placing the transformed data into unix-file.txt. The original data in dos-file.txt is not changed. (The tr command is discussed in more detail in Chapter 5.)
The > redirector creates the destination