Saturday, 7 March 2026

Use case of printf

 

Why printf Instead of echo?

While echo is commonly used for printing text, printf provides better control over:

  • Newlines

  • Alignment

  • Field width

  • Numeric formatting

  • Writing formatted output to variables

Because of this, printf is preferred in production shell scripts.


Basic Usage of printf

Printing Without a Newline

[root@oel01db ~]# printf 'hello world'
hello world[root@oel01db ~]#

Notice the prompt appears immediately after the output because no newline was printed.


Printing With a Newline

[root@oel01db ~]# printf 'hello world\n'
hello world
[root@oel01db ~]#

\n explicitly prints a newline.


Printing Multiple Values

Using %s Placeholder

%s represents a string placeholder.

[root@oel01db ~]# printf '%s \n' hi there
hi
there
[root@oel01db ~]#

Each argument is printed using the same format.


Printing Multiple Strings on One Line

[root@oel01db ~]# printf '%s %s\n' hi there
hi there
[root@oel01db ~]#

Here:

  • First %shi

  • Second %sthere


Controlling Field Width

One powerful feature of printf is field width formatting.

Right Alignment (Default)

[root@oel01db ~]# printf '%10s %10s\n' hi there
hi there
[root@oel01db ~]#

10 means minimum width of 10 characters.

If the string is shorter, spaces are added to the left.


Left Alignment

[root@oel01db ~]# printf '%-10s %-10s\n' hi there
hi there
[root@oel01db ~]#

- tells printf to left align the text.


Visualizing Padding

[root@oel01db ~]# printf '<%-10s> <%-10s>\n' hi there
<hi > <there >
[root@oel01db ~]#

Using <> helps visualize extra padding spaces.


Dynamic Width Formatting

Instead of hardcoding the width, we can pass it dynamically.

[root@oel01db ~]# printf '<%-*s>\n' 8 there

< there>

[root@oel01db ~]#

* tells printf:

Take the field width from the next argument.

Example again:

[root@oel01db ~]# printf '<%-*s>\n' 8 there
<there >
[root@oel01db ~]#

This is extremely useful when building tables dynamically.


Working with Numbers

printf also supports numeric formatting.

Basic Integer

[root@oel01db ~]# printf '%d\n' 77
77
[root@oel01db ~]#

%d prints an integer.


Minimum Width

[root@oel01db ~]# printf '%5d\n' 77
77
[root@oel01db ~]#

Number is padded with spaces.


Left Align Numbers

[root@oel01db ~]# printf '%-5d\n' 77
77
[root@oel01db ~]#

Zero Padding

[root@oel01db ~]# printf '%05d\n' 77
00077
[root@oel01db ~]#

This is useful for:

  • log file numbering

  • formatted IDs

  • timestamps


Writing printf Output to a Variable

printf has a very useful -v option.

Instead of printing to stdout, it stores the result in a variable.

[root@oel01db ~]# printf -v var 'hello %s' mahesh
[root@oel01db ~]#
[root@oel01db ~]#
[root@oel01db ~]# echo $var
hello mahesh
[root@oel01db ~]#

This is cleaner and faster than command substitution.

Example you often see:

var=$(printf 'hello %s' mahesh)

But printf -v avoids spawning a subshell.


Practical Use Cases of printf in Shell Scripts

1. Creating Table-Like Output

Example:

printf '%-10s %-10s\n' NAME SCORE
printf '%-10s %-10d\n' Alice 90
printf '%-10s %-10d\n' Bob 80

Output:

NAME SCORE
Alice 90
Bob 80

2. Formatting Log Messages

printf '[%s] %s\n' "$(date +%T)" "Service started"

Example output:

[10:22:45] Service started

3. Zero-Padded File Numbers

Useful for sequential files:

printf 'file_%03d.log\n' 1

Output:

file_001.log

4. Formatting Floating Numbers

Another useful format specifier:

printf '%.2f\n' 3.14159

Output:

3.14

This is useful in monitoring scripts or calculations.

No comments:

Post a Comment

JFrog Artifactory - How to install

JFrog Artifactory OSS Installation Guide CentOS 9 + PostgreSQL 17 This guide provides a structured workflow to install JFrog Artifactory OSS...