Greetings,
Hope someone is having an okay Summer; I'm ready for Fall.
PDKsh question:
Ref: PD KSH v5.2.14 99/7/13.2-a (default Ksh on NetBSD 11.0)
I'm wondering if there is a way -- in pdksh -- to accomplish
backslash expansion, i.e. '\r', in patterns used for parameter substitution?-a Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
What appears to work:
$ REPLY=fubar^M
$ REPLY="${REPLY%$(print '\r')}"
print $REPLY |cat -v
fubar
Entering a literal ^M after the "%" also works.
What doesn't work (but I'd like it to):
$ REPLY=fubar^M
$ REPLY="${REPLY%$'\r'}"
print $REPLY |cat -v
fubar^M
The above works in Bash and MirKsh, likely Ksh93 too so I was
somewhat surprised it failed with PDKsh.
Tried numerous ways
to quote '\r' but none worked so I'm inclined to believe that
backslash expansion is not a thing for substitution patterns
in PDKsh -- am I wrong ?
-tdc
Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
What doesn't work (but I'd like it to):
$ REPLY=fubar^M
$ REPLY="${REPLY%$'\r'}"
The above works in Bash and MirKsh, likely Ksh93 too so I was
somewhat surprised it failed with PDKsh.
On 2026-08-11, Top Dead Ctr <tdc@invalid.invalid> wrote:
Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
Side note: That's weird because there shouldn't be a trailing
carriage return in the first place.
PDKsh question:
Ref: PD KSH v5.2.14 99/7/13.2 (default Ksh on NetBSD 11.0)
I'm wondering if there is a way -- in pdksh -- to accomplish
backslash expansion, i.e. '\r', in patterns used for parameter
substitution? Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
What appears to work:
$ REPLY=fubar^M
$ REPLY="${REPLY%$(print '\r')}"
print $REPLY |cat -v
fubar
Entering a literal ^M after the "%" also works.
What doesn't work (but I'd like it to):
$ REPLY=fubar^M
$ REPLY="${REPLY%$'\r'}"
print $REPLY |cat -v
fubar^M
The above works in Bash and MirKsh, likely Ksh93 too so I was
somewhat surprised it failed with PDKsh. Tried numerous ways
to quote '\r' but none worked so I'm inclined to believe that
backslash expansion is not a thing for substitution patterns
in PDKsh -- am I wrong ?
Top Dead Ctr <tdc@invalid.invalid> writes:
[...]
I'm wondering if there is a way -- in pdksh -- to accomplish
backslash expansion, i.e. '\r', in patterns used for parameter
substitution? Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
[...]
Why do you care about '\r' characters? In normal usage, you
shouldn't see '\r' characters unless you add them explicitly, as
you have your sample code above. Are you reading Windows text files?
[...]
On 2026-08-12 01:28, Keith Thompson wrote:
Top Dead Ctr <tdc@invalid.invalid> writes:
[...]
I'm wondering if there is a way -- in pdksh -- to accomplishWhy do you care about '\r' characters? In normal usage, you
backslash expansion, i.e. '\r', in patterns used for parameter
substitution? Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
[...]
shouldn't see '\r' characters unless you add them explicitly, as
you have your sample code above. Are you reading Windows text files?
Mind that there is no such beast as "normal usage" in our domain.
IETF Internet protocols' data may contain '\r'. Web-pages/content
may contain '\r'. Yes, and DOS/Windows sources, or old (pre-OS X)
Apple data.
Sometimes you are even surprised by unexpected and dis-functional
behavior:
$ cat cr
ls *
$ sh cr
ls: cannot access '*'$'\r': No such file or directory
one might expect a shell to behave more tolerant to line endings
but, OTOH, ^M ('\r', CR) is primarily just "data".
On 2026-08-11, Top Dead Ctr <tdc@invalid.invalid> wrote:
Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
Side note: That's weird because there shouldn't be a trailing
carriage return in the first place.
On 2026-08-11 21:39, Top Dead Ctr wrote:
Greetings,
Hope someone is having an okay Summer; I'm ready for Fall.
PDKsh question:
Disclaimer: I haven't touched PDksh for decades!
(Maybe you find some useful hints below anyway.)
Ref: PD KSH v5.2.14 99/7/13.2-a (default Ksh on NetBSD 11.0)
I'm wondering if there is a way -- in pdksh -- to accomplish
backslash expansion, i.e. '\r', in patterns used for parameter
substitution?-a Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
Note that 'REPLY' is the standard target for read (no need to
specify it, REPLY can be omitted) - it's sensible to use your
own variable names. (Just BTW.)
What appears to work:
$ REPLY=fubar^M
$ REPLY="${REPLY%$(print '\r')}"
print $REPLY |cat -v
fubar
Entering a literal ^M after the "%" also works.
What doesn't work (but I'd like it to):
$ REPLY=fubar^M
$ REPLY="${REPLY%$'\r'}"
Yes, "ANSI Strings" like $'\r' is what I'd have suggested with
other shells (ksh, bash, zsh)!
If PDksh does not support it... - what do you expect now?
You could fix your data and pass your data through a 'tr -d'
pipeline.
Or if your interface is the 'read' command you may set IFS=^M
to redefined the separator for the respective read command
-a IFS=^M read -r
Or, portably, use a variable to store the ^M using 'printf'
-a cr=$( printf "\r" )
then use "${cr}" where you need it; e.g. REPLY=${REPLY%${cr}}
Historically PDksh was a comparably lousy shell; and I wouldn't
touch it. (I don't know whether it got better lately.)-a Usually
Bash was faster adopting such features from original Ksh of else.
Tried numerous ways
to quote '\r' but none worked so I'm inclined to believe that
backslash expansion is not a thing for substitution patterns
in PDKsh -- am I wrong ?
Sorry I cannot suggest PDksh specifics; inspect its man page.
(If you can replace PDksh-a I strongly suggest to do so.)
On 8/11/26 3:01 PM, Janis Papanagnou wrote:
[...]
-a-a IFS=^M read -r
I sort of like this approach of eliminating ^M at the source.
[...]
Or, portably, use a variable to store the ^M using 'printf'
-a-a cr=$( printf "\r" )
then use "${cr}" where you need it; e.g. REPLY=${REPLY%${cr}}
This does work but as there is generally just the one trailing ^M
it seems to me I might as well use $(print '\r') and skip creating
the CR variable.
I would definitely use a read-only var if it got
used multiple times.
Regarding filter piping through tr, cut, sed,
etc.,-a if I start seeing embedded ^M or other control characters
I'll likely need to use that approach.
[...]
Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
Side note: That's weird because there shouldn't be a trailing
carriage return in the first place.
You certainly meant a trailing _newline_.
$ printf "Hello world\r\n" | read x; printf "$x" | od -c
On 2026-08-11, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
Specifically I'd like to natively strip off
the trailing carriage return from 'read -r REPLY'.
Side note: That's weird because there shouldn't be a trailing
carriage return in the first place.
You certainly meant a trailing _newline_.
No, I meant a carriage return. Where does it come from? The
original phrasing "the trailing carriage return" makes it sound as
if this was perfectly expected. It isn't.
$ printf "Hello world\r\n" | read x; printf "$x" | od -c
And you have to go out of your way to insert one here.
I agree, it's lacking and NetBSD should consider including a
better shell as part of their distribution.-a In my usage case
I'm trying to stick with just the tools provided in the base
install and pdksh is arguably the best of the 3 (ash and csh
being the other 2).
On 12/08/2026 10:59 PM, Top Dead Ctr wrote:
I agree, it's lacking and NetBSD should consider including aThen shouldn't the /proper solution/ be to just fix the pdksh in
better shell as part of their distribution.-a In my usage case
I'm trying to stick with just the tools provided in the base
install and pdksh is arguably the best of the 3 (ash and csh
being the other 2).
NetBSD?-a I admit curiousity about this operating system, so I'm
probably also going to use pdksh for a bit; at least until I'd in-
stall zsh and/or tcsh which are my two favorite interactive shells.
And if you want me to look into updating pdksh for this purpose, can
you please re-summarize the problem in terms of a bug report and/or
a feature request for pdksh?-a While I may look into fixing pdksh, I'm
not in the mood to go through all the discussion and try to divine what
the actual problem is about in the first place.
Best wishes, and happy kshing!
On 8/12/26 9:45 PM, Johann 'Myrkraverk' Oskarsson wrote:
On 12/08/2026 10:59 PM, Top Dead Ctr wrote:
I agree, it's lacking and NetBSD should consider including aThen shouldn't the /proper solution/ be to just fix the pdksh in
better shell as part of their distribution.-a In my usage case
I'm trying to stick with just the tools provided in the base
install and pdksh is arguably the best of the 3 (ash and csh
being the other 2).
NetBSD?-a I admit curiousity about this operating system, so I'm
probably also going to use pdksh for a bit; at least until I'd in-
stall zsh and/or tcsh which are my two favorite interactive shells.
And if you want me to look into updating pdksh for this purpose, can
you please re-summarize the problem in terms of a bug report and/or
a feature request for pdksh?-a While I may look into fixing pdksh, I'm
not in the mood to go through all the discussion and try to divine what
the actual problem is about in the first place.
Best wishes, and happy kshing!
That's a generous offer.-a I'm not really sure how open the NetBSD
developers would be to extending the version of pdksh distributed
in the base system.-a They DO ship an extended version of The One
True AWK, adding gensub() and some time functions from Gawk. You
could inquiry on one of the various mlists (you don't need to be
subscribed to post); https://netbsd.org/mailinglists/ .-a Probably
posting to "netbsd-users" is a good starting point.
The NetBSD pkgsrc system (not NetBSD-specific but primarily used
with NetBSD) has a packaged version of pdksh so simply submitting
a patch might be the best way of offering pdksh extensions which
the developers can scrutinize and hopefully fold into the base distribution.-a FYI: currently LLM-generated code policy is that
it's not allowed.
I've packaged up a few pkgsrc packages over the years so if you
really want to take this on I'm willing to do that task.-a pkgsrc
in the past was strictly available by CVS but Git and I think Hg
is also an option; the pkgsrc pdksh package is here:
https://cdn.netbsd.org/pub/pkgsrc/current/pkgsrc/shells/pdksh/index.html
There are no patches currently applied to the 23 year old source
tarball so yours could be the first!
As to the _what_ of said extensions, I think it's essentially
adding the following to the parameter expansion feature set:
-a1) backslash expansion, ie. '\r' => ^M
-a2) //[%#]<pat>/<repl>/ style substitutions
I've no idea how hard this would and I'm actually fine with just
making use of the current features in pdksh as I'm a hobbyist and
actually kind of enjoy finding work-arounds for old software.
I'm not really sure how open the NetBSD developers would be to
extending the version of pdksh distributed in the base system.
I'm not really sure how open the NetBSD developers would be to
extending the version of pdksh distributed in the base system.
Is this different from the other BSDs? Is there no common upstream
that they all draw from?
On 2026-08-13, Lawrence DrCOOliveiro <ldo@nz.invalid> wrote:
I'm not really sure how open the NetBSD developers would be to
extending the version of pdksh distributed in the base system.
Is this different from the other BSDs? Is there no common upstream
that they all draw from?
The last common upstream was 4.4BSD Lite in 1994 or so. (Yes, that
simplifies a much messier history.)
That came with a version of the Almquist shell. FreeBSD has reworked
that shell extensively since then. OpenBSD replaced it with pdksh in
1996, which IIRC offered better Bourne/POSIX conformance at the
time.
On Fri, 14 Aug 2026 17:18:08 -0000 (UTC), Christian Weisgerber wrote:
On 2026-08-13, Lawrence DrCOOliveiro <ldo@nz.invalid> wrote:
I'm not really sure how open the NetBSD developers would be to
extending the version of pdksh distributed in the base system.
Is this different from the other BSDs? Is there no common upstream
that they all draw from?
The last common upstream was 4.4BSD Lite in 1994 or so. (Yes, that
simplifies a much messier history.)
That came with a version of the Almquist shell. FreeBSD has reworked
that shell extensively since then. OpenBSD replaced it with pdksh in
1996, which IIRC offered better Bourne/POSIX conformance at the
time.
I had a look to see what was available on Debian, and found this <https://packages.debian.org/trixie/mksh>:
mksh is the successor of the Public Domain Korn shell (pdksh), a
Bourne/POSIX compatible shell which is largely similar to the
original AT&T Korn Shell (ksh88/ksh93). It includes bug fixes and
feature improvements, in order to produce a modern, robust shell
good for interactive and especially script use. mksh has UTF-8
support (in string operations and the Emacs editing mode). The
code has been cleaned up and simplified, bugs fixed, standards
compliance added, and several enhancements (for extended
compatibility to other modern shells, as well as a couple of its
own) are available. This shell is Debian Policy 10.4 compliant and
works as /bin/sh on Debian systems (use the /bin/lksh executable)
and is a good rescue and initrd shell (consider the
/bin/mksh-static executable).
Perhaps a better basis for ongoing use than old pdksh code?
Supposedly MKsh (MirBSD Ksh) is the default shell in Android. AFAIK,
all the main BSDs (Free;Open;Net) ship with PDKsh;
My next task is then to install NetBSD in a VM.-a I expect I'll do that coming Monday.-a The reason I can do all this is because I'm currently between jobs, and this will be a nice warmup before I start the next dayjob.-a Also, I'm curious if I can do this, and if the NetBSD develop-
ers will accept my changes.
So assuming no hickups, I'll check in on Monday after booting up NetBSD.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 50:07:47 |
| Calls: | 1,100 |
| Files: | 1,339 |
| Messages: | 275,859 |