Kaz Kylheku wrote:
> On 2026-11-17, Christian Weisgerber <nad…@mips.inka.de> wrote: >
>> Various printf(1) implementations attempt option parsing on the first >> argument and choke on something like "printf ---": >> >> FreeBSD sh(1) builtin: printf: Illegal option -- >> FreeBSD standalone: printf: illegal option -- - >> bash(1) builtin: -bash: printf: --: invalid option >> >> POSIX does not specify any options for printf(1).
> > However, POSIX does still specify the -- option for printf; it's just > not spelled out in the description of printf. > > Contrast this situation with that of the echo utility. For echo, > much more is written than just "OPTIONS ... None": > > The echo utility shall not recognize the "--" argument in the manner > specified by Guideline 10 of XBD 12.2 Utility Syntax Guidelines; "--" > shall be recognized as a string operand. > > Implementations shall not support any options. > > The printf utility is in no way similarly excused from supporting --, > nor forbidden from having options courtesy of the implementor.
Just to back this up with evidence, the requirement for printf derives from XCU 1.4 Utility Description Defaults under OPTIONS: Default Behavior: When this section is listed as "None.", it means that the implementation need not support any options. Standard utilities that do not accept options, but that do accept operands, shall recognize "--" as a first argument to be discarded. The requirement for recognizing "--" is because conforming applications need a way to shield their operands from any arbitrary options that the implementation may provide as an extension. For example, ... https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap01.html#tag_18_04
> (Implementations of echo, such as the Bash builtin and the coreutils > echo, ignore the proscription against supporting options.)
I believe they conform to POSIX in this respect. Their documentation may well call them options, but what they are allowed to do by POSIX is dependent on the contents of the first argument (which according to POSIX is an operand): The following operands shall be supported: string A string to be written to standard output. If the first operand consists of a '-' followed by one or more characters from the set {'e', 'E', 'n'}, or if any of the operands contain a <backslash> character, the results are implementation-defined. (This is followed by some XSI-shaded text requiring that implementations supporting the XSI option always treat string operands as a string to be written, and specifying how backslash-escapes are processed.) Since only e, E and n produce implementation-defined behaviour, attempting to use other "options" should cause the string to be written, and this seems to be what they do: bash$ echo -x foo -x foo bash$ /bin/echo -x foo -x foo Although coreutils printf accepts long options when you don't have POSIXLY_CORRECT set: bash$ echo --version --version bash$ POSIXLY_CORRECT=1 /bin/echo --version --version bash$ /bin/echo --version echo (GNU coreutils) 9.1 Copyright (C) 2022 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Brian Fox and Chet Ramey.
-- Geoff Clare <netn…@gclare.org.uk>