Home > Text Processing & Search > printf

printf: Formatted Output

`printf` is a command similar to the C language's `printf` function, which outputs data to standard output according to a specified format string. It allows you to format variables or literal values in various ways (integers, floating-point numbers, strings, etc.) and insert special characters using escape sequences. It offers more precise output control than the `echo` command.

Overview

`printf` precisely controls output using a format string and arguments. It is utilized in scripts for various purposes such as report generation and log message formatting.

Key Features

  • Supports various format specifiers (e.g., %s, %d, %f)
  • Supports escape sequences (e.g., \n, \t, \x)
  • Outputs data types such as integers, floating-point numbers, and strings
  • Allows control over output width, precision, and alignment

Main Options (Format Specifiers and Escape Sequences)

The `printf` command controls its behavior not through traditional options starting with `-`, but through 'format specifiers' and 'escape sequences' embedded within the format string. Here are some commonly used ones.

Format Specifiers

Escape Sequences

Generated command:

Try combining the commands.

Description:

`printf` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Examples of using `printf` with various format specifiers and escape sequences.

Basic String Output

printf "Hello, World!\n"

Outputs a simple string and a newline.

Mixed Integer and String Output

printf "Name: %s, Age: %d years\n" "Hong Gildong" 30

Formats and outputs an integer and a string together.

Floating-point Precision Specification

printf "Pi: %.2f\n" 3.141592

Outputs a floating-point number with two decimal places.

Output Width and Alignment

printf "%-10s %5d\n" "Product Name" 123

Left-aligns a string in a 10-character width and right-aligns an integer in a 5-character width.

Hexadecimal and Octal Output

printf "Decimal: %d, Hex: %x, Octal: %o\n" 255 255 255

Outputs numbers in hexadecimal and octal formats.

Using with Variables

NAME="Kim Cheolsu"
SCORE=95
printf "Student: %s, Score: %d points\n" "$NAME" "$SCORE"

Outputs shell variables as arguments to `printf`.

Tips & Precautions

Tips and points to note for effective use of `printf`.

Difference from `echo`

`echo` is mainly used for simple string output and has limitations in interpreting escape sequences (requires `-e` option) or formatting. In contrast, `printf` allows precise control over output format through format specifiers and always interprets escape sequences.

  • Escape Sequences: `printf` always interprets escape sequences. `echo` requires the `-e` option.
  • Format Specification: `printf` supports various format specifiers like `%s`, `%d`, etc., enabling fine-grained control over output formatting. `echo` lacks this functionality.
  • Newline: `echo` adds a newline by default after output, whereas `printf` requires explicit specification of `\n`.

Matching Format String and Arguments

The number of format specifiers in the format string must match the number of arguments provided. If arguments are insufficient, default values (e.g., 0 or an empty string) may be used depending on the shell. If there are too many arguments, the excess arguments are ignored. This can lead to unexpected results, so caution is advised.

Caution with Escape Sequences

Specifically, to output a `\` character, you must use it twice, like `\\`. The shell might interpret the backslash first, so escaping might be necessary before passing it to `printf`. Generally, enclosing the format string in double quotes (`"`) is safer.


Same category commands