Article View: comp.unix.shell
Article #406Re: POSIX: printf ---?
From: Geoff Clare
Date: Wed, 18 Nov 2026 13:21
Date: Wed, 18 Nov 2026 13:21
91 lines
4500 bytes
4500 bytes
Kaz Kylheku wrote:
> On 2026-11-17, Christian Weisgerber <naddy@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 <netnews@gclare.org.uk>
Technical Details
Message-ID:
<7cl0tm-bib.ln1@ID-313840.user.individual.net>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mailReferences:
<slrn11fp4hu.sql.naddy@lorvorc.mips.inka.de> <20261117122806.362@kylheku.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Geoff Clare <geoff@clare.See-My-Signature.invalid> Newsgroups: comp.unix.shell Subject: Re: POSIX: printf ---? Date: Wed, 18 Nov 2026 13:21:43 +0000 Lines: 91 Message-ID: <7cl0tm-bib.ln1@ID-313840.user.individual.net> References: <slrn11fp4hu.sql.naddy@lorvorc.mips.inka.de> <20261117122806.362@kylheku.com> Reply-To: netnews@gclare.org.uk Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net WxEpJVgEuUVsX0Mn2BVybwrzgm/ZrERUm9eaC2mgUs9Oi/6HWA X-Orig-Path: ID-313840.user.individual.net!not-for-mail Cancel-Lock: sha1:4zyVpAzZyUfLpHMem5qmxKPm/bM= sha256:vOY1yDmMYmoaTAoMWxs1QQaTDo9kX+Ck+Xxv8HVEhsc= User-Agent: Pan/0.154 (Izium; 517acf4)