You are currently viewing Linux Commands – printf

Linux Commands – printf

Linux Commands – printf

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed echo command in Linux which is used to print in standard output in Linux.

In this blog post, we will discuss printf command in Linux. printf command is used to print a formatted string in the standard output while interpreting ‘%’ directives and ‘\’ escapes to format numeric and string arguments in a way that is mostly similar to the C ‘printf’ function.

Format-Control Letters:

A format specifier starts with the character ‘%’ and ends with a format-control letter—it tells the printf statement how to output one item. The format-control letter specifies what kind of value to print. The rest of the format specifier is made up of optional modifiers that control how to print the value, such as the field width. Here is a list of the format-control letters.

Format Details
%a A floating point number of the form [-]0xh.hhhhp+-dd (C99 format).
%A Same as %a, only prints uppercase instead of lowercase letters.
%b Print the associated argument while interpreting backslash escapes in there
%c Print a number as a character; thus, ‘printf “%c”, 65’ outputs the letter ‘A’. The output for a string value is the first character of the string.
%d Print a decimal integer.
%i Same as %d, for compatibility with ISO C.
%e Print a number in scientific (exponential) notation.
%E Same as %e, only prints uppercase instead of lowercase letters.
%f Print a number in floating-point notation.
%F Same as %f, but the infinity and “not a number” values are spelled using uppercase letters.
%g Print a number in either scientific notation or in floating-point notation, whichever uses fewer characters.
%G Same as %g, only uses uses ‘E’ instead of ‘e’.
%n Assigns the number of characters printed so far to the variable named in the corresponding argument.
%o Print an unsigned octal integer.
%q Print the associated argument shell-quoted, reusable as input
%s Print a string.
%u Print an unsigned decimal integer.
%x Print an unsigned hexadecimal integer.
%X Same as %x, only uses the letters ‘A’ through ‘F’ instead of ‘a’ through ‘f’.
%% Print a single ‘%’.


Modifiers for printf Formats:

A format specification can also include modifiers that can control how much of the item’s value is printed, as well as how much space it gets. The modifiers come between the ‘%’ and the format-control letter. Here are the possible modifiers, in the order in which they may appear.

Modifiers Details
N$ An integer constant followed by a ‘$’ is a positional specifier.
The minus sign, used before the width modifier, says to left-justify the argument within its specified width. Normally, the argument is printed right-justified in the specified width.
space For numeric conversions, prefix positive values with a space and negative values with a minus sign.
+ The plus sign, used before the width modifier, says to always supply a sign for numeric conversions, even if the data to format is positive. The ‘+’ overrides the space modifier.
# Use an “alternative form” for certain control letters. For ‘%o’, supply a leading zero. For ‘%x’ and ‘%X’, supply a leading ‘0x’ or ‘0X’ for a nonzero result. For ‘%e’, ‘%E’, ‘%f’, and ‘%F’, the result always contains a decimal point. For ‘%g’ and ‘%G’, trailing zeros are not removed from the result.
0 A leading ‘0’ (zero) acts as a flag indicating that output should be padded with zeros instead of spaces. This applies only to the numeric output formats. This flag only has an effect when the field width is wider than the value to print.
A single quote or apostrophe character is a POSIX extension to ISO C. It indicates that the integer part of a floating-point value, or the entire part of an integer decimal value, should have a thousands-separator character in it. This only works in locales that support such characters.


Linux Commands – printf:

Hope you have enjoyed this article. In the next blog post, we will discuss cat command in Linux.


Leave a Reply