fortypoundhead.com

PRINTF

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


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

SYNTAX
printf format [argument]...

printf --help

printf --version

OPTIONS

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)

\UNNNNNNNN
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.

Examples

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

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:54.91.38.173

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




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers