Linux printf Command: Print Values with a Defined Format

printf prints strings and numbers according to a format string. It is useful in shell scripts because it can control line breaks and column widths more precisely than echo.

What is the printf command?

In Bash, printf is a shell builtin, but an external printf from GNU Coreutils may also exist. You can check which implementation is used by running type printf. Since there is no automatic newline, include \n in the format string if needed.

Basic syntax

printf 'format string' [values...]

The installed implementation and options may vary depending on the distribution. Check the current system's documentation with man printf.

Examples

Strings and line breaks

Place values in %s and end the line with \n.

printf 'Hello, %s\n' Linux

Aligning column widths

Set left-alignment widths and number widths to create a simple table.

printf '%-12s %5d\n' apple 42

Repeating multiple values output

The format string is repeatedly applied to the remaining arguments.

printf '%s\n' one two three

Comparing Format Strings and Actual Output

printf inserts arguments into the format specifiers in order. %-6s displays a string left-aligned with a width of 6, and %03d fills an integer with zeros up to three digits.

printf '[%-6s] [%03d]\n' apple 7

Example output:

[apple ] [007]

The \n at the end of the format string is a line break. Unlike echo, printf does not guarantee automatic line breaks, so specify it if needed. If user input is placed directly in the format string, % or backslashes could be interpreted, so use a fixed format like printf '%s\n' "$value".

Main Options and Format

Options/Format Description
%s Prints the value as a string.
%d Prints an integer in decimal.
%f Prints a floating-point number with the specified precision.
\n Represents a line break within a format string.
%-Ns Left-aligns a string within at least N spaces.

Precautions when using

If user input is passed as the format string itself, % specifications are interpreted. To safely print the value, use a fixed format like printf '%s\n' "$value". Quote shell variables to prevent word splitting and wildcard expansion.

Frequently Asked Questions

Does printf automatically add a newline at the end like echo?

No. To change lines, you need to insert \n directly into the format string.

Official Documentation

You can check the exact behavior of the options and implementation differences in the official documentation for printf.

For the behavior of external commands, also refer to the official GNU Coreutils manual.

More in This Category
Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux host Command: Look Up DNS Names and Addresses

Linux host Command: Look Up DNS Names and Addresses

Learn how to look Up DNS Names and Addresses with the Linux host command, including practical examples, key options, and important precautions.

Linux Tutorial / File System Basics: Paths and Key Directories

Linux Tutorial / File System Basics: Paths and Key Directories

Understand the Linux directory tree, read absolute and relative paths, and learn what common directories such as /home, /etc, and /var are used for.

Linux whereis Command: Locate Binaries, Source, and Man Pages

Linux whereis Command: Locate Binaries, Source, and Man Pages

Learn how Linux whereis locates binaries, source files, and manual pages, limits each search type, lists effective paths, and differs from which and find.

Linux watch Command: Run a Command Repeatedly

Linux watch Command: Run a Command Repeatedly

Learn how to use the Linux watch command to rerun a command at regular intervals and highlight changing output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux sync Command: Flush Buffered Writes to Storage

Linux sync Command: Flush Buffered Writes to Storage

Learn how to flush Buffered Writes to Storage with the Linux sync command, including practical examples, key options, and important precautions.

Linux cut Command: Extract Characters and Fields

Linux cut Command: Extract Characters and Fields

Learn how to extract Characters and Fields with the Linux cut command, including practical examples, key options, and important precautions.

Linux Directory Structure Explained: Purpose of Major Directories

Linux Directory Structure Explained: Purpose of Major Directories

Understand the purpose of major Linux directories such as /etc, /var, /usr, /home, /proc, and /sys, and learn where common system files belong.

Linux Tutorial / How the Shell Runs Commands: ;, &&, ||, Aliases, and Environment Variables

Linux Tutorial / How the Shell Runs Commands: ;, &&, ||, Aliases, and Environment Variables

Learn how Bash resolves commands and uses exit status with ;, &&, and ||, then distinguish aliases, shell variables, and exported environment variables.

Linux printf Command: Print Values with a Defined Format

Linux printf Command: Print Values with a Defined Format

Learn how to print Values with a Defined Format with the Linux printf command, including practical examples, key options, and important precautions.