Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

printf %q broken with size specifier #1477

Open
@samliddicott

Description

@samliddicott

korn man page printf section says:
%q A %q format can be used instead of %s to cause the resulting string to be quoted in a manner than can be reinput to the shell.

$ printf '%q\n' "a'b"
$'a'b'

$ printf '%.2q\n' "a'b"
$'

definitely not suitable for re-input to the shell.

GNU Coreutils printf %q refuses to accept size specifiers for %q:
$ /usr/bin/printf '%.2q\n' "a'b"
/usr/bin/printf: %.2q: invalid conversion specification

bash has the same bug. Their response is "programmer error" "Don't do that" but I think the error is with the bash maintainers, and like the GNU Coreutils maintainers they should not let bash do that. If the shell maintainers who know about this bug won't "not do that" how can their users who don't know about it be expected to "not do that"?

%q should not renege on it's guarantees of suitability for re-use by the shell.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions