Bash Printf Function Tutorial with Examples

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Bash provides programming features to make Linux system administrator’s life easier. printf is one of the most used function used by Linux bash programmers. In this tutorial we will look Linux bash printf features and use cases while developing bash programs. Bash printf function is very similar to the C printf function.

Just Print

We can use printf function easily by providing the text in double quotes. In this example we will print Hello Poftut .

Print Given Text

We can provide  parameters to print text. We will use %s as text specifier in printf function.

Print Given Text

Print Given Text

Print Multiple Given Text

We can also provide multiple parameters to the printf  command. In this example we will print to the Poftut , ismail ,ahmet .

This will print

Print Multiple Given Text

Print Multiple Given Text

Print Integer Numbers

We can also print integer type values with bash printf function. We will use %d to print integer values. In this example we will print the age variable.

This will print

Print Integer Numbers

Print Integer Numbers

Print Float Numbers

We can print float type values with bash printf function too. We will use %f to print float values. In this example we will print the price variable.

This will print

LEARN MORE  How To Check If File Exists In Linux Bash?

Format Specifiers

We can use format specifiers to print given text or numeric values. We will can specify the floating point decimal part and floating point part with . . In this example we will print only 2 numbers from floating point parts.

This will print

Format Specifiers

Format Specifiers

Justify Left

We may need to align the given text to the left. In this situation we will use - to justify left.

This will print

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

You may also like...

Leave a Reply

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

Open

Enjoy this blog? Please spread the word :)