Re: your $(#…) bugreport

mirabilos <[email protected]> Mon, 26 Jan 2026 21:06:31 +0000 (UTC)
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
Hi Jari,

thanks for tracking me down in IRC about this. Unfortunately,
a netsplit ate my answer. I’m writing from an eMail account that
*should* work with Googlemail, but I don’t use this for FOSS
communication normally.

13:15⎜<jaalto:#!/bin/mksh> ATTN: mksh maintainer, bug found in
     ⎜    process substitution parsing () with comments
13:18⎜<jaalto:#!/bin/mksh> Also found in Bash (reported), Ksh (bug
     ⎜    report in-progress), Dash (reported). See bug report at
     ⎜    https://pastebin.com/4LD95GJS

(Also, ouch. Please do not capitalise any letter in “mksh”, TYVM.
If you absolutely must have something in title case, write it out
as “MirBSD Korn Shell” instead. See the FAQ.)


The thing is: the præ-POSIX accent-gravis-style command substitution
is not identical to the POSIX $(…) comsub. For example, the user may
not use double quotes both outside and inside, as the results differ
between shells there (and yes, POSIX allows that, and it is good to
do so).

You cannot use an accent-gravis inside a string either, as it is
taken as the closing one:

$ PS2='[PS2] '
$ echo foo`echo bar'`'baz`
[PS2]

The contents of $(…) comsubs are parsed as full commands, using a
recursive parser, to avoid the pdksh bug of…

 (mksh) $ echo $(case foo in bar) echo miau ;; esac)

(pdksh) $ echo $(case foo in bar) echo miau ;; esac)
pdksh: syntax error: `;;' unexpected

So, the closing parenthesis for POSIX-style comsubs is searched
for only at the top level of the expression inside. The docs
support this:

| With the $(commands) form, all characters following the open
| parenthesis to the matching closing parenthesis constitute the
| commands string.
|
| With both the backquoted and $(commands) forms, the commands string
| shall be tokenized (see [153]2.3 Token Recognition) and parsed […]

Token Recognition has:

| 9. If the current character is a '#', it and all subsequent characters
| up to, but excluding, the next <newline> shall be discarded as a
| comment.

And the accent-gravis-style comsubs have special rules:

| The search for the matching backquote shall be satisfied by the first
| unquoted non-escaped backquote; during this search, if a non-escaped
| backquote is encountered within a shell comment, a here-document, an
| embedded command substitution of the $(commands) form, or a quoted
| string, undefined results occur.

So, writing “`#comment`”, even if it works, is actually undefined, too.


Therefore, it’s not a bug, it’s expected behaviour.

bye,
//mirabilos
-- 
„Cool, /usr/share/doc/mksh/examples/uhr.gz ist ja ein Grund,
mksh auf jedem System zu installieren.“
	-- XTaran auf der OpenRheinRuhr, ganz begeistert
(EN: “[…]uhr.gz is a reason to install mksh on every system.”)