قالب وردپرس درنا توس
Home / Tips and Tricks / How to use printf in Bash – CloudSavvy IT

How to use printf in Bash – CloudSavvy IT

Shutterstock / gilangtristiano

Text formatting is an interesting topic, especially when it comes to Bash. While there are many tools for formatting text, printf is unique and universal. Learn all about printf in Bash to format your text just the right way!

Using printf in Bash

C ++ and other developers will already be familiar with printf as a great way to format text output while programming. Having the same tool available in Bash is a big advantage. printf allows you to manage and produce complex output formats just the way you want them. The printf manual page defines printf as a ̵

6;format and print data’ tool, which is exactly what it is and does.

For example, do you want to print a decimal-based number with an exact length of 3 bytes (or characters / digits) for the integer part (the part before the decimal point or comma, depending on where in the world you live and what your locale is) and 2 bytes (or characters / digits) for the decimal part of the number? No problem, printf can do it and much more. It can also send the text to a specific column on the screen, etc.

While printf’s syntax may look confusing or complicated to a new developer, it’s actually quite simple. There is only a limited set of commonly used formatting characters that can be used within a printf output definition. Let’s take a look at these first.

Syntax: printf

These are the most commonly used printf syntax number formatting idioms:

%d or %i  a signed decimal integer
%u        an unsigned decimal integer
%f        a decimal floating point number
%e        a scientific notation number
%g        will let printf use %e or %f, whichever is shorter
%c        a single character
%s        a string of characters
%%        a literal '%'

Next, it is good to know how to specify specific options related to these formatting idioms. The format for specifying options for this is as follows:


Examples: printf

For example, to have a float with a width of 3 and a precision of 2, specify the following:


Let’s look at an example:

printf "%3.2fn" "100.304" 

Print a well-made float with printf

Straightforward and easy. Note that we could have used a variable instead of "100.304":

printf "%3.2fn" ${VAR1}

Print a formatted float with printf using a Bash variable

Also notice how we added a new line using a n newline series. For a full list of available interpreted sequences, see man printf. The most common are n for newline, t for tab and \ for a backslash.

In the option format above, we have also seen how to specify flags. There are a limited number of commonly used flags:

-   Left-justify text (right-justify is the default)
+   Forces the use of a sign, even if positive
' ' A space is inserted if no sign will be used
0   Left-pad the number with zeroes instead of spaces when padding is specified

For example, we can have:

printf "% 3.2ftt%+3.2fn" ${VAR1} ${VAR2}

Print negative numbers with printf and formatting

Here we have used two variables and separated the output by two tabs using two t tab strings. We have also inserted a space (as a flag) between them % and the width definition. This led to the negative number in VAR1 are printed with a minus sign. If the number had been positive, no sign would have been printed and a space would have been printed in the same position instead.

Finally, we have the + flag and indeed, our number is executed with a leading + character regardless of the fact that the number is already positive and thus (normally) no character is printed.

So how can we make sure we have leading zeros or spaces while still having a certain length? First, we need to make sure that our definition width is longer than the variable content, and second, we can set the 0 flag to display leading zeros:

printf "% 13.2fn%013.2fn%13.2f" ${VAR1} ${VAR2} ${VAR1}

Output format for printf and leading zeros definition

In this example we will print 3 lines separated twice by the n newline series. We immediately note that if there is no newline at the end of the output, our Bash prompt returns specifically at the end of our third line of output.

This might not be a nice or useful format in interactive mode in the terminal, but from a script (think of a string like 1...2...3... which is built step by step on the screen as progress is made within the code) this can be very useful.

In the second line of output we use the 0 flag to indicate that we want to use leading zeros VAR2. Indeed, the output returns leading zeros, up to the defined length.

In the first line of the output we have inserted a space, while in the third line of the output (both print the VAR1 variable) we don’t have. The output is the same which is the only quirk I have ever found while working printf; you would expect there to be an extra character given the printf space flag definition. This remains the same even if we used leading zeros. It also remains the same if no length is specified.

Thus, the usability of the space flag may be limited in some cases (negative numbers). This is not the case for positive numbers:

printf "% fn%0fn" ${VAR1} ${VAR1}

A leading space in positive numbers based on the special space flag

In this case (positive numbers), the space flag works as expected, and an extra space is inserted in the case of the first VAR1 output.


Using printf from Bash scripts or at the command line can produce clear, well-defined output. If you scale variables correctly (and maybe range, check them to be sure), use printf gives you a flexible way to format the output (and overall screen layout for larger applications) as you see fit.

In this article, we’ll look at commonly used number formatting idioms, how to define the width of both integer and decimal parts of numbers, and take a look at the most commonly used flags in printf. Enjoy printing well-formatted data with printf!

Source link