printf Command in Linux with Examples

This tutorial explains Linux “printf” command, options and its usage with examples.

printf – format and print data

DESCRIPTION

‘printf’ prints the FORMAT string, interpreting `%’ directives and `\’ escapes to format numeric and string arguments in a way that is mostly similar to the C `printf’ function.

SYNOPSIS

printf FORMAT [ARGUMENT]…

FORMAT controls the output as in C printf. Interpreted sequences are:

advertisement
advertisement

\”
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
and all C format specifications ending with one of diouxXfeEgGcs, with ARGUMENTs converted to proper type first. Variable widths are handled.

EXAMPLES

1. Simple usage example

$ printf “hello printf”
hello printf$

At this point we have supplied and argument “hello”. Not the different behaviour in comparison to echo command. No new line had been printed out as it it in case of when using default setting of echo command.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

2. To print a new line we need to supply printf with format string with escape sequence \n ( new line ):

$ printf "%s\n" "hello printf"
hello printf

The format string is applied to each argument:

$ printf "%s\n" "hello printf" "in" "bash script"
hello printf
in
bash script

3. Format specifiers

advertisement

As you could seen in the previous simple examples we have used %s as a format specifier. The most commonly used printf specifiers are %s, %b, %d, %x and %f .

Instead of %s specifiers we can use %b specifier which is essentially the same by it allows us to interpret escape sequences with an argument:

$ printf "%s\n" "1" "2" "\n3"
1
2
\n3
$ printf "%b\n" "1" "2" "\n3"
1
2
 
3

When it comes to printing a integers we can use %d specifier:

advertisement
$ printf "%d\n" 255 0xff 0377 3.5
255
255
255
bash: printf: 3.5: invalid number

To printf floating point numbers a %f specifier is our friend:

$ printf "%f\n" 255 0xff 0377 3.5
255.000000
255.000000
377.000000
3.500000

The default behaviour of %f printf specifier is to print floating point numbers with 6 decimal places. To limit a decimal places to 1 we can specify a precision in a following manner:

$ printf "%.1f\n" 255 0xff 0377 3.5
255.0
255.0
377.0
3.5

Formatting to three places with preceding with 0 will require format like “%03d”.

Formatting strings with number of characters also happens in the same way i.e. %3s format will lead to printing of string of 3 characters.

4. Command substitution

$ now="$(date)"
$ printf "Current date and time %s\n" "$now"

5. Sample srcipt example of a table

Format names to 7 places nad max 7 characters and format floating point number to 9 places with 2 decimals. More complicated sample script using printf formatting to create a table with multiple items. Save as a script make executable and run:

# /bin/bash
divider===============================
divider=$divider$divider
 
header="\n %-10s %8s %10s %11s\n"
format=" %-10s %08d %10s %11.2f\n"
 
width=43
 
printf "$header" "ITEM NAME" "ITEM ID" "COLOR" "PRICE"
 
printf "%$width.${width}s\n" "$divider"
 
printf "$format" \
Triangle 13  red 20 \
Oval 204449 "dark blue" 65.656 \
Square 3145 orange .7

Output:

$ ./table 
 
 ITEM NAME   ITEM ID      COLOR       PRICE
===========================================
 Triangle   00000013        red       20.00
 Oval       00204449  dark blue       65.66
 Square     00003145     orange        0.70

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.