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
.
printf "Hello Poftut"
Print Given Text
We can provide parameters to print text. We will use %s
as text specifier in printf function.
printf "Hello %s" "Poftut"

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
.
printf "Hello %s " "Poftut" "ismail" "ahmet"
This will print

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.
printf "My age is %d" 22
This will print

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.
printf "Price is %f" 1.99
This will print
Price is 1.990000
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.
printf "Price is %2.2f" 1.98765
This will print

Justify Left
We may need to align the given text to the left. In this situation we will use -
to justify left.
printf "% -2.2f" 1.98765
This will print
1.99