printf

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package printf implements the printf builtin command.

printf — format and print data

Usage: printf FORMAT [ARGUMENT]...

Write formatted output to standard output. FORMAT is a string that contains literal text and format specifiers (introduced by %). Each format specifier consumes the next ARGUMENT and formats it.

If there are more ARGUMENTs than format specifiers, the FORMAT string is reused from the beginning until all arguments are consumed (bounded to 10,000 iterations to prevent runaway loops).

Missing arguments default to "" for string specifiers and 0 for numeric specifiers.

Accepted flags:

--help
    Print a usage message to stdout and exit 2.

Rejected flags:

-v varname
    Bash extension to assign output to a variable. Not supported
    in the restricted shell.

Format specifiers:

%s     String.
%b     String with backslash escape interpretation (like echo -e).
       \c in %b stops all further output.
%c     First character of the argument.
%d, %i Signed decimal integer.
%o     Unsigned octal integer.
%u     Unsigned decimal integer.
%x, %X Unsigned hexadecimal integer (lower/upper).
%e, %E Scientific notation float.
%f, %F Decimal float.
%g, %G Shortest float representation.
%%     Literal percent sign.

Width and precision modifiers are supported (e.g. %10s, %-10s, %.5f, %010d). Flag characters: - (left-align), + (sign), ' ' (space), 0 (zero-pad), # (alternate form).

Escape sequences in FORMAT string:

\\    backslash
\a    alert (BEL)
\b    backspace
\f    form feed
\n    newline
\r    carriage return
\t    horizontal tab
\v    vertical tab
\"    double quote
\NNN  octal byte value (1-3 digits)
\0NNN     octal byte value (0 + 1-3 digits)
\xHH     hexadecimal byte value (1-2 digits)
\uHHHH   Unicode code point (1-4 hex digits)
\UHHHHHHHH Unicode code point (1-8 hex digits)

Numeric argument extensions:

Arguments for numeric specifiers may be:
- Decimal integers: 42, -7, +3
- Octal: 0755
- Hexadecimal: 0xff, 0XFF
- Character constants: "'A" or '"A' gives the ASCII value of A

Not implemented (rejected):

%n     Byte count write (security risk). Produces an error.
%q     Shell-quoting (bash extension, not POSIX).
%a, %A Hexadecimal float (deferred).

Exit codes:

0  Successful completion (conversion warnings may still be emitted).
1  Format error (invalid number, unknown specifier, incomplete specifier).
2  Usage error (no format string provided).

Memory safety:

printf does not read files or stdin. All output is generated from
the format string and arguments. The format reuse loop is bounded
to maxFormatIterations (10,000) and checks ctx.Err() on each
iteration to honour the shell's execution timeout.

Index

Constants

This section is empty.

Variables

View Source
var Cmd = builtins.Command{
	Name:        "printf",
	Description: "format and print data",
	Help:        `printf: usage: printf [-v var] format [arguments]`,
	MakeFlags:   builtins.NoFlags(run),
}

Cmd is the printf builtin command descriptor. printf uses NoFlags because its arguments (format string and data) can look like flags (e.g. printf "%d" -42). Manual pre-parsing handles --help and -v.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL