Bug 274650 - sh does not accept -- after -c
Summary: sh does not accept -- after -c
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 13.2-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-10-22 16:25 UTC by Guillem Jover
Modified: 2024-05-05 20:05 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Guillem Jover 2023-10-22 16:25:56 UTC
After having seen a similar change implemented for glibc, I did the same in dpkg to make its shell invocations more robust and avoid missparsing in case a command starts with a «-» (which would be very unusual, but…). See <https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=f013195c70995235340e99107058f591175f0a57>.

Just noticed afterwards that this did not work on FreeBSD's /bin/sh:

  ,---
  $ sh -c -- "echo \$0" name arg
  echo $0: --: not found
  `---

This though seems to work fine on posh, dash, ksh93, bash, and the default sh on NetBSD, OpenBSD, Solaris and macOS. But neither on sh, ksh nor bsh on AIX 7.3.

For now I guess I'll need to make its usage conditional on the system.
Comment 1 Benjamin Takacs 2023-10-22 16:55:33 UTC
FreeBSDs sh doesn't do any option parsing after -c, this makes accidents due to an unusual nammed command impossible and lead to the behaviour you observe.

sh tries to run the command ´--´ and uses ´echo $0´ providing ´arg´ as argument.
As you don't have any command named ´--´ on your system sh reports it with its name ´echo $0´.

That is POSIX incompatiple behaviour. I'm in favour of documenting this incompatibility and closing this report as ´Works As Intended´
Comment 2 Cristian Rodriguez 2024-02-17 22:43:23 UTC
Hit this one, is there a way to detect this broken shell somehow ? posix says you should accept --. POSIX TCs (future standards)  says system(3) and popen(3) must behave as 
execve sh -c -- command too. makes impossible to write a command line that behaves equally on all posix shells.
Comment 3 Jilles Tjoelker freebsd_committer freebsd_triage 2024-04-30 20:03:27 UTC
It should work to prepend a space to the command string.
Comment 4 Guillem Jover 2024-05-01 03:51:41 UTC
I'm not sure I understand your comment about prepending a space. Could you clarify with an example invocation?
Comment 5 Jilles Tjoelker freebsd_committer freebsd_triage 2024-05-05 20:05:50 UTC
sh -c ' --' attempts to invoke the '--' utility regardless of the interpretation of '-c'.

This does have a side effect of breaking echoed parts of the command (as seen in bash syntax errors) or column numbers, but it is only necessary if the command starts with '-'.