O'Reilly logo

Linux and Unix Shell Programming by David Tansley

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

printf

Format:

printf format arguments 

This command prints formatted text to the standard output, and is similar to awk ’s printf function.

Format:

Format can contain three different types of items; we will only look at formatting. The sequence to format is:

%[- +]m.nx 

where the hyphen will left-justify a field. Generally speaking the m is used to represent the length of a field and n is the maximum length of the field.

The % sign is preceded with any of the following to format characters:

s string
c character
d decimal
x hexadecimal number
o octal number

Printf by itself does not generate a new line; you have to use the escape sequence for that. Here is a list of the most common escape sequences.

\a sound the bell
\b backspace
\r carriage ...

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