Printf Command
Printf: Format and Print Data
The printf command is a powerful utility for formatting
and printing data to standard output. It is commonly found as a
shell built-in in shells like Bash, but a standalone GNU version
also exists. This tool offers a more controlled and portable way to
display information compared to the simpler
echo command.
Shell Scripting with Printf
printf is invaluable in shell scripting for creating
dynamic filenames, displaying user information, and generating
formatted output. For instance, you can construct a timestamped
filename for backups using
printf -v FileName 'Backup_%(%F_%X)T.tgz' -1. This
command assigns the formatted date and time to the
FileName variable, making it suitable for archival
purposes.
Basic Data Formatting with Printf
Displaying simple text with newlines is straightforward. The command
printf '%s\n' "$USER" prints the current user's
username followed by a newline character. This is a fundamental use
case for ensuring clean and readable output in scripts.
Advanced Number Formatting
printf excels at formatting numbers. To output integers
from 1 to one million with comma separators for readability, you can
use printf "%'d\n" {1..1000000}. This feature
significantly enhances the human readability of large numerical
data. For zero-padding, ensuring a fixed width of 3 characters, use
printf '%#.3d\n' 12. To space-pad a number to a width
of 3 characters, use printf '%3d\n' 12. Left-aligning
the number with space padding on the right is achieved by prefixing
the width with a hyphen: printf '%-3d\n' 12.
Dynamic Field Spacing
When dealing with fields of varying lengths,
printf allows for dynamic spacing. By using a variable
for the field width, such as
printf '%*s\n' $Integer 'Example Field', you can ensure
consistent alignment and presentation of data, which is crucial for
tabular output or structured logs.
# printf
# Format and print data
# This command is typically available as a built-in to many shells, such as the
# Bourne shell and the Bourne Again Shell. However, there also exists a GNU
# alternative, sometimes found over at `/usr/bin/printf`.
# Assign the current date (timestamp style) as a shell variable, using the Bash
# builtin, and make it a suitable filename for a Gzip-compressed Tar archive.
printf -v FileName 'Backup_%(%F_%X)T.tgz' -1
# Simple, feature-full, and portable way by which to echo(1) output to STDOUT.
# Here, the current user's username is displayed, followed by a new line.
printf '%s\n' "$USER"
# Using the Bash builtin, this will output one integer per line, from one to
# one million, in a human-readable kind of way, by appropriately
# comma-separating the units.
printf "%'d\n" {1..1000000}
# Getting these results by using the comma is actually also viable in AWK, but
# you'll likely have to jump through a quotation hoop to get access to it.
# Zero-pad a number in order to maintain a width of 3 characters. It's also
# possible to instead provide a `0` in-place of the hash (`#`).
printf '%#.3d\n' 12
# As above, but instead, space-pad the number. Prefix the `3` with a hyphen
# (`-`) to left-align the number, causing the padding to occur on the right.
printf '%3d\n' 12
# Set a field's spacing by using an integer provided as a variable. This is
# incredibly useful when you're dealing with inconsistent field lengths.
printf '%*s\n' $Integer 'Example Field'