Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

man printf, info printf and printf --help are all pretty useless to me; I want an explanation of the FORMAT options. For example, I could use:

printf '%s\n' foo bar

and get output:

foo
bar

From general reading online I surmise that %s means something like 'separator', so with it printf prints every argument provided to it, separated by newlines.

I've also seen references here and there to %d and a number of others (I think it's something to do with digits?). Could someone please provide an overview of these formatting options?

share|improve this question
    
as an aside, an overview of the formatting options (or conversion specifiers) would be unnecessarily verbose for an answer, in my opinion. –  Josh McGee Jul 23 '13 at 19:34
    
My basic expectation would be that it follows standard printf functionality from the c library. %s is string, %d is decimal, %f is floating point. As per the answer, it does with some extended capability. –  BillThor Jul 23 '13 at 23:39
add comment

2 Answers

up vote 3 down vote accepted

The best resource I've found is the bash-hackers.org wiki for all things related to Bash. It's what the man pages should be instead of massive pile that's tough to navigate.

There are specific topics on the builtins including printf. This page is exhaustive! It includes all the formatting options as well as examples and even a discussion section at the bottom for helping to flesh our corner cases and nuances with printf's functionality.

I've also been known to use the GNU Coreutils documentation on printf, specifically the page on the formatting options, in a pinch.

share|improve this answer
add comment

For bash, the primary resource is man bash. For builtins specifically, there is the help builtin. Here is a quote from help printf.

In addition to the standard format specifications described in printf(1) and printf(3), printf interprets:

 %b        expand backslash escape sequences in the corresponding argument
 %q        quote the argument in a way that can be reused as shell input
 %(fmt)T output the date-time string resulting from using FMT as a format
       string for strftime(3)

The printf(N) notation used in the help printf means that you should refer to the manual section of the command denoted by the number in parentheses.

See man 1 printf, man 3 printf, and if you want man 3 strftime for the special %(fmt)T specifier.

You have to piece it all together yourself; the documentation does not simply list "%s is for string, %d is for digit." I believe the primary thing you are asking for is known as the conversion specification.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.