O'Reilly logo

Unix in a Nutshell, 4th Edition by Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Output Redirections

For print and printf, dest-expr is an optional expression that directs the output to a file or pipe.

> file

Directs the output to a file, overwriting its previous contents.

>> file

Appends the output to a file, preserving its previous contents. In both of these cases, the file will be created if it does not already exist.

| command

Directs the output as the input to a system command.

|& command

Directs the output as the input to a coprocess. gawk only.

Be careful not to mix > and >> for the same file. Once a file has been opened with >, subsequent output statements continue to append to the file until it is closed.

Remember to call close() when you have finished with a file, pipe, or coprocess. If you don't, eventually you will hit the system limit on the number of simultaneously open files.

printf Formats

Format specifiers for printf and sprintf have the following form:

    %[posn$][flag][width][.precision]letter

The control letter is required. The format conversion control letters are given in the following table.

Character

Description

c

ASCII character.

d

Decimal integer.

i

Decimal integer. (Added in POSIX)

e

Floating-point format ([-]d.precision e[+-]dd).

E

Floating-point format ([-]d.precision E[+-]dd).

f

Floating-point format ([-]ddd.precision).

g

e or f conversion, whichever is shortest, with trailing zeros removed.

G

E or f conversion, whichever is shortest, with trailing zeros removed.

o

Unsigned octal value.

s

String. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required