At Mon, 15 Dec 2025 22:54:13 +0000, void <[email protected]> wrote:
Subject: Re: PS1 and other things for superuser
>
> On Mon, Dec 15, 2025 at 10:32:02PM +0000, RVP wrote:
> > The right way to do this in NetBSD's /bin/sh is:
> >
> > ```
> > set -o promptcmds
> > PSlit=$(printf '\1')
> > PS1='$(date +"%a %b %e  %H:%M:%S")${PSlit}$(tput bold setaf 1)${PSlit} 
> > $USER@$(hostname) $(pwd) ${PSlit}$(tput sgr0)${PSlit} # '
> > ```

Wow, that's _expensive_!

There's magic support in KSH, including pdksh, aka NetBSD /bin/ksh, to
set the time (and maybe the date) based on an expression expanding from
$SECONDS that avoids any command expansion:

17:11 [2637] $ echo $PS1
${_x[(_m=_mm)==(_h=_hh)]}$_h:$_m [${LEV:+${LEV}.}!] $

The magic is in setting SEONDS to the number of seconds since the last
local wall-clock midnight hour:

        SECONDS=$(eval expr $(date '+3600 \* %H + 60 \* %M + %S'))

Then setting some expressions to make the PS1 setting more readable:

     _hh="(SECONDS/3600)%24"
     _mm="(SECONDS/60)%60"
     _ss="(SECONDS)%60"

This all comes from, I think, a Usenet post by David Korn but it must
have been before 1993 at least since that's when these appeared in what
remains of my ~/.kshrc history -- anything earlier was lost I think.

Bash needs a printf, but if that's a builtin it's just a fork, not also
an exec.

(I also put the $PWD in the window banner to keep $PS1 short.)

> > Note that there's no '\n' in that PS1. Don't know how to get a literal '\n'
> > into PS1.

In NetBSD /bin/sh and /bin/ksh it can be done easily by just embedding a
newline in the string, i.e. hit enter before closing the string:

        PS1='$PWD
        $ '

I'm pretty sure it would work the same for most other Bourne-like shells
too.

> I'll need to unlearn 'doas su -' from muscle memory.

That would be my first suggestion!

Personally I just use plain "su", not anything more, and I do set root's
shell to /bin/ksh, and that allows me to share my $ENV with the root
session.  I keep a root shell window open so I don't have to keep typing
the password every time I need to do something privileged.  It's a bit
less secure, but of course "doas" is even less secure.  Any user who has
the root password is a good target for a trojan horse.

All the gory, and full of historic baggage, details here:

        https://github.com/robohack/dotfiles/

I keep toor's shell as /bin/sh, but on most of my systems that's also
static linked binary.  I don't (like to) do dynamic-linked systems.

--
                                        Greg A. Woods <[email protected]>

Kelowna, BC     +1 250 762-7675           RoboHack <[email protected]>
Planix, Inc. <[email protected]>     Avoncote Farms <[email protected]>

Attachment: pgpQqqFiJhcvH.pgp
Description: OpenPGP Digital Signature

Reply via email to