printf

text processingLinux/Unix
The printf command is one of the most frequently used commands in Linux/Unix-like operating systems. printf Format and print data

Quick Reference

Command Name:

printf

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

printf [options] [arguments]

Common Use Cases

    Syntax

    printf FORMAT [ARGUMENT]...

    Options

    Format Specifier Description
    %d, %i Print an integer as a signed decimal number
    %u Print an integer as an unsigned decimal number
    %f Print a floating-point number
    %e, %E Print a floating-point number in scientific notation
    %g, %G Print a floating-point number in either normal or scientific notation, whichever is more appropriate
    %x, %X Print an integer as an unsigned hexadecimal number
    %o Print an integer as an unsigned octal number
    %s Print a string
    %c Print a character
    %% Print a literal % character
    \n Newline
    \t Tab
    \r Carriage return
    \\ Backslash
    \ooo Character with octal value ooo
    \xHH Character with hexadecimal value HH

    Examples

    How to Use These Examples

    The examples below show common ways to use the printf command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    printf "Hello, World!\n"
    Print a simple string with a newline at the end.
    printf "Name: %s\nAge: %d\n" "John" 25
    Print formatted text with string and integer values.
    printf "PI: %.2f\n" 3.14159
    Print a floating-point number with 2 decimal places. # Advanced Examples Advanced printf "%10s %5d %7.2f\n" "Name" "Age" "Salary" printf "%10s %5d %7.2f\n" "John" 25 1250.50 printf "%10s %5d %7.2f\n" "Jane" 32 1850.75 Print a formatted table with aligned columns. printf "Hex: %x\nOctal: %o\n" 255 255 Print decimal number 255 in hexadecimal and octal formats. printf "ASCII character: %c\n" 65 Print the ASCII character corresponding to the code 65 (A). printf "%s\0" "null-terminated string" | hexdump -C Create a null-terminated string and view it in hexadecimal. printf "Progress: [%-20s] %d%%\n" "##########" 50 Create a simple progress bar (10/20 blocks filled, 50%). for i in {0..100..10}; do printf "\rProgress: [%-10s] %3d%%" "$(printf '#%.0s' $(seq 1 $(($i/10))))" $i sleep 0.2 done Print an animated progress bar.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `printf` command is a powerful formatting tool in Unix/Linux systems that allows for precise control over output formatting. It's modeled after the C programming language's printf() function and serves as a more versatile alternative to the `echo` command when specific formatting is required. Unlike `echo`, which simply prints its arguments followed by a newline, `printf` interprets format specifiers and escape sequences to produce formatted output. This makes it particularly useful for: 1. Creating formatted tables with aligned columns 2. Displaying numbers in various formats (decimal, hexadecimal, octal, scientific notation) 3. Controlling decimal precision in floating-point numbers 4. Generating output without trailing newlines (useful for progress indicators) 5. Creating specially formatted data for processing by other programs 6. Producing exact output for scripts where precision matters The `printf` command takes a format string as its first argument, followed by values that will be inserted into the format string. The format string can contain: - Ordinary characters that are printed as-is - Escape sequences like \n (newline), \t (tab), and \r (carriage return) - Format specifiers that begin with % and define how subsequent arguments should be formatted Format specifiers can include modifiers that control field width, precision, alignment, and padding. For example: - `%5d` prints an integer in a field at least 5 characters wide, right-aligned - `%-10s` prints a string in a field at least 10 characters wide, left-aligned - `%.2f` prints a floating-point number with exactly 2 decimal places - `%08x` prints a hexadecimal number padded with leading zeros to make it 8 digits wide The `printf` command is both a shell built-in command in Bash and other shells, and a standalone utility in the GNU Coreutils package. While the functionality is similar, there might be slight differences between implementations. Proficient use of `printf` is a valuable skill for shell scripting, especially when generating reports, processing data, or creating user interfaces in terminal-based applications. Its precision and control make it preferred over `echo` in many scripting scenarios where exact formatting is important.

    Related Commands

    These commands are frequently used alongside printf or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the printf command works in different scenarios.

    $ printf
    View All Commands