The way to Use printf in Bash – CloudSavvy IT

Posted on


Textual content formatting is an fascinating matter, particularly on the subject of Bash. While there are various instruments for textual content formatting, printf is exclusive and common. Be taught all about printf in Bash to format your textual content simply the precise method!

Utilizing printf in Bash

C++ and different builders will already be accustomed to printf as a good way to format the output of textual content when programming. To have the identical instrument obtainable in Bash is of nice profit. printf means that you can management and produce advanced output codecs, simply the way in which you need it. The printf handbook web page defines printf as a ‘format and print information’ instrument, which is precisely what it’s and does.

For instance, would you prefer to print a decimal based mostly quantity with a precise size of three bytes (or characters/numbers) for the integer half (the half earlier than the decimal dot or comma, relying on the place on this planet you reside and what your locale is) and a couple of bytes (or characters/numbers) for the decimal a part of the quantity? No downside, printf can do it and rather more. It might additionally output the textual content to a particular column on the display screen, and so forth.

Although the syntax of printf, for a brand new developer, might look complicated or advanced, it’s truly fairly simple. There are solely a restricted set of generally employed formatting characters which can be utilized inside a printf output definition. Let’s take a look at these first.

Syntax: printf

These are probably the most generally used printf syntax quantity formatting idioms:

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

Subsequent, it’s good to know the best way to specify particular choices in reference to these formatting idioms. The format for specifying choices for these is as follows:


Examples: printf

So for instance, to have a float with a width of three, and a precision of two, you’d specify:


Let’s take a look at an instance:

printf "%3.2fn" "100.304" 

Printing a well formatted float with printf

Easy and simple. Word that we might have used a variable as an alternative of "100.304":

printf "%3.2fn" ${VAR1}

Printing a formatted float with printf using a Bash variable

Word additionally how we added a newline by utilizing a n newline sequence. For a full listing of accessible interpreted sequences, see man printf. The most typical ones are n for newline, t for tab and for a backslash.

Within the choices format above we additionally noticed how we will specify flags. There are a restricted quantity of frequent flags:

-   Left-justify textual content (right-justify is the default)
+   Forces using an indication, even when optimistic
' ' An area is inserted if no signal will likely be used
0   Left-pad the quantity with zeroes as an alternative of areas when padding is specified

Thus, for instance, we will have:

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

Printing negative numbers with printf and formatting

Right here we used two variables, and separated the output by two tabs utilizing two t tab sequences. We additionally inserted an area (as a flag) in between % and the width definition. This led to the detrimental quantity in VAR1 to be printed utilizing a detrimental signal. If the quantity would have been optimistic, no signal would have been printed, and as an alternative an area can be printed in the identical place.

Lastly we used the + flag and certainly our quantity is output with a number one + signal no matter the truth that the quantity is already optimistic and thus (usually) no signal can be printed.

So how can we be certain that we’ve got main zeros or areas, whereas nonetheless having a particular size? First, we’ve got to make sure our definition width is longer then variable contents, and secondly we will use the 0 flag to point out main zeros:

printf "% 13.2fnpercent013.2fnpercent13.2f" ${VAR1} ${VAR2} ${VAR1}

Output formatting for printf and leading zeroes definition

On this instance, we print 3 strains, separated two occasions by the n newline sequence. We instantly observe that if there is no such thing as a newline on the finish of the output, that our Bash immediate returns particularly on the finish of our third line of output.

This might not be a pleasant or useful format in interactive mode within the terminal, however from inside a script (suppose for instance a few sequence like 1...2...3... which builds step-by-step on the display screen as progress throughout the code is made) this may be very useful.

Within the second line of output, we use the 0 flag to point we want to use main zeroes for VAR2. The output certainly yields main zeroes, as much as the outlined size.

Within the first line of output, we inserted an area whereas within the third line of output (each printing the VAR1 variable) we didn’t. The output is similar, which is the one oddity I ever discovered whereas working with printf; one would count on there to be one extra character given the printf area flag definition. This stays the identical even when we might use main zeroes. It additionally stays the identical if no size is specified.

The usability of the area flag might thus in some instances (detrimental numbers) be restricted. This isn’t the case for optimistic numbers:

printf "% fnpercent0fn" ${VAR1} ${VAR1}

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

On this case (optimistic numbers), the area flag works as anticipated, and an additional area is inserted within the case of the primary VAR1 output.


Utilizing printf from inside Bash scripts or on the command line can yield clear well-defined output. In the event you scale variables appropriately (and maybe vary verify them for certainty), utilizing printf offers you a versatile methodology to format the output (and general display screen structure for bigger functions) as you select.

On this article we take a look at generally used quantity formatting idioms, the best way to outline the width of each the integer and decimal a part of numbers, and reviewed probably the most generally used flags in printf. Take pleasure in printing well-formatted information with printf!

Source link

Gravatar Image
I love to share everything with you

Leave a Reply

Your email address will not be published. Required fields are marked *