-
-
Notifications
You must be signed in to change notification settings - Fork 2
Linux printf Guide
Mattscreative edited this page Dec 5, 2025
·
3 revisions
Complete beginner-friendly guide to printf on Linux, covering Arch Linux, CachyOS, and other distributions including formatted output, string formatting, and output control.
Basic usage:
# Formatted output
printf "Hello %s\n" "World"
# Output: Hello WorldFormat and arguments:
# Format string with arguments
printf "Name: %s, Age: %d\n" "John" 30
# Output: Name: John, Age: 30Basic formats:
# %s = string
printf "%s\n" "Hello"
# %d = decimal integer
printf "%d\n" 42
# %f = floating point
printf "%f\n" 3.14Formatting:
# Width
printf "%10s\n" "Hello"
# Precision
printf "%.2f\n" 3.14159
# Width and precision
printf "%10.2f\n" 3.14159String formats:
# Basic string
printf "%s\n" "Hello World"
# Left align
printf "%-20s\n" "Hello"
# Right align
printf "%20s\n" "Hello"String padding:
# Zero padding
printf "%05d\n" 42
# Output: 00042
# Space padding
printf "%5d\n" 42
# Output: 42Integer types:
# Decimal
printf "%d\n" 42
# Octal
printf "%o\n" 42
# Hexadecimal
printf "%x\n" 42
# Uppercase hex
printf "%X\n" 42Float formats:
# Default float
printf "%f\n" 3.14
# Scientific notation
printf "%e\n" 3.14
# Shorter of %f or %e
printf "%g\n" 3.14Check installation:
# printf is part of coreutils
# Usually pre-installed
# Check printf
which printfThis guide covered printf usage, formatted output, and string formatting for Arch Linux, CachyOS, and other distributions.
- echo Guide - Text output
- Bash Scripting Guide - Scripting basics
- Text Processing - Text manipulation
-
printf Documentation:
man printf
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.