🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
5 messages
5 total messages Started by Christian Weisgerber Tue, 17 Nov 2026 17:43
POSIX: printf ---?
#394
Author: Christian Weisgerber
Date: Tue, 17 Nov 2026 17:43
27 lines
1669 bytes
Any POSIX lawyers around?

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).

The workaround is to explicitly terminate option parsing with "--",
i.e., "printf -- ---".  That works at least across OpenBSD, FreeBSD,
GNU coreutils and bash.  But does it conform to POSIX?


The problem isn't hypothetical.  In release 0.131, Game of Tree's
regression test suite added things like

+       printf '--- gamma/delta\t\n' >> $testroot/stdout.expected
+       printf '+++ gamma/delta\t\n' >> $testroot/stdout.expected

which promptly blew up on FreeBSD and presumably some flavors of
Linux.

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de
Re: POSIX: printf ---?
#395
Author: Kaz Kylheku
Date: Tue, 17 Nov 2026 20:46
72 lines
4023 bytes
On 2026-11-17, Christian Weisgerber <naddy@mips.inka.de> wrote:
> Any POSIX lawyers around?

I'm supposed to be in court, but it was unexpectedly adjourned.

> Various printf(1) implementations attempt option parsing on the first
> argument and choke on something like "printf ---":

There is a "--" argument which means "this is the last option; only
ordinary non-option arguments follow". Obviously, "---" is not that
argument.

> 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.

(Implementations of echo, such as the Bash builtin and the coreutils
echo, ignore the proscription against supporting options.)

> The workaround is to explicitly terminate option parsing with "--",
> i.e., "printf -- ---".  That works at least across OpenBSD, FreeBSD,
> GNU coreutils and bash.  But does it conform to POSIX?

I believe yes; though POSIX does not specify any required options
for printf, the utility conforms to the common guidelines.

The description of printf itself has a FUTURE DIRECTIONS section where
it is spelled out that implementations can add options:

  A future version of this standard is expected to add a %b conversion to
  the printf() function for binary conversion of integers, in alignment
  with the next version of the ISO C standard. This will result in an
  inconsistency between the printf utility and printf() function for
  format strings containing %b. Implementors are encouraged to collaborate
  on a way to address this which could then be adopted in a future version
  of this standard. For example, the printf utility could add a -C option
  to make the format string behave in the same way, as far as possible, as
  the printf() function.

printf is allowed to treat arguments that start with - as options,
and diagnose nonexistent options.

Highly portable scripts must take care not to use format strings
that start with -.

Also, do not pass the expansion of a variable to printf:

  printf "$TEXT\n"       # no

  printf "%s\n" "$TEXT"  # yes

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Re: POSIX: printf ---?
#396
Author: Lawrence D’Oliveiro
Date: Tue, 17 Nov 2026 21:37
20 lines
1862 bytes
On Tue, 17 Nov 2026 17:43:26 -0000 (UTC), Christian Weisgerber 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

The GNU coreutils version prints the “---” without error.

> The workaround is to explicitly terminate option parsing with "--",
> i.e., "printf -- ---".  That works at least across OpenBSD, FreeBSD,
> GNU coreutils and bash.  But does it conform to POSIX?

Or how about

    printf %s ---

Or just use echo and be done with it.
Re: POSIX: printf ---?
#406
Author: Geoff Clare
Date: Wed, 18 Nov 2026 13:21
91 lines
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>
Re: POSIX: printf ---?
#407
Author: Geoff Clare
Date: Wed, 18 Nov 2026 15:57
18 lines
1464 bytes
I wrote:

> Although coreutils printf accepts long options when you don't have

I've no idea how I ended up writing "coreutils printf" here instead
of "coreutils echo".  Brains are weird.

> POSIXLY_CORRECT set:
> 
> bash$ echo --version
> --version
> bash$ POSIXLY_CORRECT=1 /bin/echo --version
> --version
> bash$ /bin/echo --version
> echo (GNU coreutils) 9.1

-- 
Geoff Clare <netnews@gclare.org.uk>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads