Posted On 2007-05-02 by FortyPoundHead
Keywords: Command Reference
Tags: Linux Commandline Linux
Views: 1501

Format and print data.
Write the formatted arguments to the standard output under the control of the format.

printf format [argument]...

printf --help

printf --version


The format characters and their meanings are:

\" double quote

\0NNN character with octal value NNN (0 to 3 digits)

\\ backslash

\a alert (BEL)

\b backspace

\c produce no further output

\f form feed

\n new line

\r carriage return

\t horizontal tab

\v vertical tab

\xNNN byte with hexadecimal value NNN (1 to 3 digits)

\uNNNN character with hexadecimal value NNNN (4 digits)

character with hexadecimal value NNNNNNNN (8 digits)

%% a single %

%b ARGUMENT as a string with `\'' escapes interpreted

%Wd Integer `W'' digits wide xxxx

%W.De Scientific notation x.xxxx e nnn. float, double

%W.Df Fixed format xx.xxxx. float, double

%W.Dg Variable `W'' digits wide,`D'' decimals xxxx.xx

%q Output the corresponding argument in a format that can be
reused as shell input

%s Character string char

and all C format specifications ending with one of diouxXfeEgGcs, with
ARGUMENTs converted to proper type first. Variable widths are handled.
e.g. `\0ooo'' = an octal number, `\xhhh'' = a hex numberThe format is a character string which contains three types of objects:

Plain characters, which are simply copied to standard output,
Character escape sequences, which are converted and copied to standard output,
Format specifications, each of which causes printing of the next successive argument.
The format is reused as necessary to consume all of the arguments. If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied.

The return value is zero on success, non-zero on failure.


$ printf "Two separate\nlines\n" # Using \n to start a new line
Two separate

printf "Hello, $USER.\n\n" # Text followed by variable $USER

printf "Distance = %5d Miles" $distance # Display an integer variable
Distance = 27 Miles

# Print text interspersed with command results
printf "This is `uname -s` running on a `uname -m` processor.\n\n"

About the Author

FortyPoundHead has posted a total of 1974 articles.


Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

List of Shady Characters
dwirch posted on April 25, 2017 at about 16:39 in Webmaster Stuff

Job Spammer: Bilal Uddin
dwirch posted on April 25, 2017 at about 11:00 in Spammers

Bug Fix: Contact Form Error
dwirch posted on April 21, 2017 at about 11:38 in Site News

Bug Fix: Ophion Time Tracker
dwirch posted on April 9, 2017 at about 11:30 in Site News

Job Spammer: Yogesh Kapadne
dwirch posted on March 31, 2017 at about 8:04 in Spammers

Job Spammer: Sathya Narayana
dwirch posted on March 15, 2017 at about 7:18 in Spammers