Skip to content

Linux printf Guide

Mattscreative edited this page Dec 5, 2025 · 3 revisions

Linux printf Guide

Complete beginner-friendly guide to printf on Linux, covering Arch Linux, CachyOS, and other distributions including formatted output, string formatting, and output control.


Table of Contents

  1. printf Basics
  2. Format Specifiers
  3. String Formatting
  4. Numeric Formatting
  5. Troubleshooting

printf Basics

Formatted Output

Basic usage:

# Formatted output
printf "Hello %s\n" "World"

# Output: Hello World

Format String

Format and arguments:

# Format string with arguments
printf "Name: %s, Age: %d\n" "John" 30

# Output: Name: John, Age: 30

Format Specifiers

Common Specifiers

Basic formats:

# %s = string
printf "%s\n" "Hello"

# %d = decimal integer
printf "%d\n" 42

# %f = floating point
printf "%f\n" 3.14

Width and Precision

Formatting:

# Width
printf "%10s\n" "Hello"

# Precision
printf "%.2f\n" 3.14159

# Width and precision
printf "%10.2f\n" 3.14159

String Formatting

String Output

String formats:

# Basic string
printf "%s\n" "Hello World"

# Left align
printf "%-20s\n" "Hello"

# Right align
printf "%20s\n" "Hello"

Padding

String padding:

# Zero padding
printf "%05d\n" 42

# Output: 00042

# Space padding
printf "%5d\n" 42

# Output:    42

Numeric Formatting

Integer Formats

Integer types:

# Decimal
printf "%d\n" 42

# Octal
printf "%o\n" 42

# Hexadecimal
printf "%x\n" 42

# Uppercase hex
printf "%X\n" 42

Floating Point

Float formats:

# Default float
printf "%f\n" 3.14

# Scientific notation
printf "%e\n" 3.14

# Shorter of %f or %e
printf "%g\n" 3.14

Troubleshooting

printf Not Found

Check installation:

# printf is part of coreutils
# Usually pre-installed

# Check printf
which printf

Summary

This guide covered printf usage, formatted output, and string formatting for Arch Linux, CachyOS, and other distributions.


Next Steps


This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.

Clone this wiki locally