Shell by Example: Printf POSIX + Bash

The printf command provides formatted output, similar to printf in C. It’s more portable and powerful than echo for complex formatting.

Basic printf syntax: printf FORMAT [ARGUMENTS...].

Edit
#!/bin/sh
printf "Hello, World!\n"
Output:
Hello, World!

Unlike echo, printf doesn’t add a newline automatically. Use \n to add newlines.

Edit
#!/bin/sh
printf "Line 0 without trailing newline"
printf "Line 1\n"
printf "Line 2\n"
Output:
Line 0 without trailing newlineLine 1
Line 2

%s is a format specifier for strings.

Edit
#!/bin/sh
printf "String: %s\n" "hello"
Output:
String: hello

%d is a format specifier for decimal integers.

Edit
#!/bin/sh
printf "Integer: %d\n" 42
Output:
Integer: 42

%f is a format specifier for floating point numbers.

Edit
#!/bin/sh
printf "Float: %f\n" 3.14159
Output:
Float: 3.141590

%e is a format specifier for scientific notation.

Edit
#!/bin/sh
printf "Scientific: %e\n" 1234.5
Output:
Scientific: 1.234500e+03

%x is a format specifier for hexadecimal numbers.

Edit
#!/bin/sh
printf "Hex: %x\n" 255
Output:
Hex: ff

%o is a format specifier for octal numbers.

Edit
#!/bin/sh
printf "Octal: %o\n" 64
Output:
Octal: 100

%c is a format specifier for single characters.

Edit
#!/bin/sh
printf "Character: %c\n" "A"
Output:
Character: A

%% is a format specifier for literal percent signs.

Edit
#!/bin/sh
printf "Percent: 100%%\n"
Output:
Percent: 100%

Width and precision formatting.

Edit
#!/bin/sh
echo "Width specifiers:"
printf "  |%10s|\n" "hello"  # Right-align, width 10
printf "  |%-10s|\n" "hello" # Left-align, width 10
printf "  |%10d|\n" 42       # Right-align number
printf "  |%-10d|\n" 42      # Left-align number

echo "Precision for floats:"
printf "  Default: %f\n" 3.14159265
printf "  2 decimals: %.2f\n" 3.14159265
printf "  Width + precision: %8.2f\n" 3.14159265

echo "Precision for strings (max length):"
printf "  %.5s\n" "Hello, World!" # First 5 characters
Output:
Width specifiers:
  |     hello|
  |hello     |
  |        42|
  |42        |
Precision for floats:
  Default: 3.141593
  2 decimals: 3.14
  Width + precision:     3.14
Precision for strings (max length):
  Hello

Zero-padding:

Edit
#!/bin/sh
printf "Zero-padded: %05d\n" 42
printf "Zero-padded: %08.2f\n" 3.14
Output:
Zero-padded: 00042
Zero-padded: 00003.14

Multiple arguments:

Edit
#!/bin/sh
printf "%s is %d years old\n" "Alice" 30
printf "%s is %d years old\n" "Bob" 25
Output:
Alice is 30 years old
Bob is 25 years old

Arguments cycle through format:

Edit
#!/bin/sh
printf "%s\n" "first" "second" "third"
printf "%s %s\n" "a" "1" "b" "2" "c" "3"
Output:
first
second
third
a 1
b 2
c 3

Escape sequences:

Edit
#!/bin/sh
echo "Escape sequences:"
printf "  Tab:\tafter tab\n"
printf "  Newline:\nafter newline\n"
printf "  Backslash: \\\\\n"
printf "  Single quote: \\'\n"
printf "  Bell: \a(beep)\n"
Output:
Escape sequences:
  Tab:	after tab
  Newline:
after newline
  Backslash: \
  Single quote: \'
  Bell: (beep)

Hex and octal escape codes:

Edit
#!/bin/sh
printf "  Hex (41 = A): \x41\n"
printf "  Octal (101 = A): \101\n"
Output:
  Hex (41 = A): A
  Octal (101 = A): A

Store printf output in a variable.

Edit
#!/bin/sh
result=$(printf "Value: %05d" 42)
echo "Stored: $result"
Output:
Stored: Value: 00042

Bash

Bash can store printf output directly with the -v flag.

Edit
#!/bin/bash
printf -v myvar "Value: %05d" 42
echo "Stored: $myvar"
Output:
Stored: Value: 00042

Bash

printf with arrays

Edit
#!/bin/bash
nums=(1 2 3 4 5)
printf "Number: %d\n" "${nums[@]}"
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

« String Manipulation | Line Filters »