printf - Formatted Output

Write formatted output.

Source: src/scripting/printf_builtin.f90:builtin_printf

Synopsis

printf FORMAT [ARGUMENTS...]

Description

Outputs formatted text according to FORMAT, similar to C's printf. More portable than echo -e for escape sequences.

Format Specifiers

SpecifierDescription
%sString
%d, %iSigned decimal integer
%uUnsigned decimal integer
%oUnsigned octal
%xUnsigned hexadecimal (lowercase)
%XUnsigned hexadecimal (uppercase)
%eScientific notation (lowercase e)
%EScientific notation (uppercase E)
%f, %FFloating-point
%gShorter of %e or %f
%GShorter of %E or %F
%cSingle character
%%Literal percent sign

Format Flags

Flags appear between % and the specifier:

FlagDescription
-Left-align (default is right)
+Show sign for positive numbers
Space for positive sign
0Zero-pad numbers
#Alternate form (0x for hex, etc.)
*Width/precision from argument

Width and Precision

%[flags][width][.precision]specifier
  • Width: Minimum field width
  • Precision:
    • For strings: maximum characters
    • For floats: decimal places
    • For integers: minimum digits

Escape Sequences

SequenceCharacter
\\Backslash
\aAlert (bell)
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\'Single quote
\"Double quote
\0nnnOctal value
\xHHHexadecimal value

Return Value

CodeMeaning
0Success
1Error (invalid format, write failure)

Examples

Basic formatting

printf "Hello, %s!\n" "World"
# Output: Hello, World!

printf "Name: %s, Age: %d\n" "Alice" 30
# Output: Name: Alice, Age: 30

Number formatting

# Integers
printf "%d\n" 42        # 42
printf "%5d\n" 42       # "   42" (width 5)
printf "%05d\n" 42      # 00042 (zero-padded)
printf "%-5d|\n" 42     # "42   |" (left-aligned)

# Hexadecimal
printf "%x\n" 255       # ff
printf "%X\n" 255       # FF
printf "%#x\n" 255      # 0xff

# Octal
printf "%o\n" 8         # 10
printf "%#o\n" 8        # 010

Floating-point

printf "%f\n" 3.14159       # 3.141590
printf "%.2f\n" 3.14159     # 3.14
printf "%8.2f\n" 3.14159    # "    3.14"
printf "%e\n" 1234.5        # 1.234500e+03

String formatting

printf "%s\n" "hello"       # hello
printf "%10s\n" "hello"     # "     hello"
printf "%-10s|\n" "hello"   # "hello     |"
printf "%.3s\n" "hello"     # hel (max 3 chars)

Width from argument

printf "%*s\n" 10 "hello"   # "     hello" (width=10)
printf "%.*s\n" 3 "hello"   # hel (precision=3)

Format reuse

When there are more arguments than format specifiers, the format repeats:

printf "%s\n" apple banana cherry
# apple
# banana
# cherry

printf "%d " 1 2 3 4 5
# 1 2 3 4 5

Escape sequences

printf "Line 1\nLine 2\n"
printf "Col1\tCol2\tCol3\n"
printf "Alert: \a"

Table formatting

printf "%-15s %8s %8s\n" "Name" "Size" "Date"
printf "%-15s %8d %8s\n" "file.txt" 1234 "Mar 9"
printf "%-15s %8d %8s\n" "data.csv" 56789 "Mar 8"

# Output:
# Name                Size     Date
# file.txt            1234    Mar 9
# data.csv           56789    Mar 8

Implementation Details

From printf_builtin.f90:

! Parse format string character by character
do while (fmt_pos <= len_trim(format_str))
    ch = format_str(fmt_pos:fmt_pos)
    if (ch == '%') then
        call parse_format_spec(format_str, fmt_pos, spec)
        call apply_format(spec, args(arg_index), output)
    else if (ch == '\') then
        call handle_escape(format_str, fmt_pos, output)
    else
        output = output // ch
    end if
end do

Comparison with echo

Featureechoprintf
Trailing newlineAutomaticManual (\n)
Escape sequencesRequires -eAlways
Format specifiersNoYes
PortabilityVariesConsistent

Related Commands

  • echo - Simple output
  • read - Read input

Notes

  • Always include \n for newlines; printf doesn't add one automatically
  • For portable scripts, prefer printf over echo -e
  • Arguments are converted to match format specifiers (strings to numbers, etc.)
  • Non-numeric strings become 0 when used with numeric specifiers
  • Quote arguments to preserve whitespace: printf "%s" "$var"