0

This is mostly a curiosity question that arose here.

From the man page of GNU sed 4.8 I read

       --posix

              disable all GNU extensions.

so I understand that if a code like the following works, it means that -i without argument is allowed by POSIX:

sed --posix -i -n '1,25p' *.txt

On the other hand, the same code (with or without --posix) doesn't work for MacOS' BSD sed, because that version expects -i to be followed by an argument.

I can see only two mutually exclusive possibilities:

  • GNU sed's --posix option allows more than POSIX, which means it bugged and needs a bug report
  • BSD sed is not POSIX-compliant.

What is the truth?

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 1
    https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html#tag_20_116_01 Looks like `-e`, `-f`, and `-n` are the only flags specified by POSIX. Behavior on other flags is implementation dependent. (Although a strict reading would imply that an argument like `-i` should be treated as a filename and a conforming implementation ought to bail with an error like `-i: file not found`) – William Pursell Oct 04 '21 at 16:27
  • `GNU sed's --posix option allows more than POSIX` The option `--posix` is not specified by POSIX, so if you would want `--posix` to error on non-posix flags, doing `sed --posix` should always error. It's not a bug, `--posix` is to exclude non-posix sed commands, like `T` or `W` or `1~2p`, not flags. – KamilCuk Oct 04 '21 at 16:32

1 Answers1

4

--posix refers to the sed language itself, not the command line interface:

GNU sed includes several extensions to POSIX sed. In order to simplify writing portable scripts, this option disables all the extensions that this manual documents, including additional commands.

POSIX does not specify -i, so an implementation without it can still be POSIX-conforming.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
  • The explanation makes sense, but where can I read the POSIX doesn't impose command line rules? – Enlico Oct 04 '21 at 17:53
  • POSIX does, but GNU sed `--posix` only switches the scripting language. It still allows other command line flags… such as `--posix` in the first place. POSIX generally allows implementations to do extra things, there’s no requirement to do “just” POSIX. – mirabilos Feb 16 '22 at 22:39