[COFF] What is your prompt?

Grant Taylor gtaylor at tnetconsulting.net
Sat Dec 25 03:17:07 AEST 2021


On 12/22/21 11:59 PM, Andy Kosela wrote:
> What is your prompt?
Pre-Script:  Seeing as how this is COFF and it's supposed to be a slow 
day at work (holding down the fort while others celebrate) I'll chime 
in.  What follows is what the type of thing that I'd be interested in 
reading from others.

I've been a *LONG* *TIME* advocate for having at the very minimum 1) the 
(current) username and 2) the hostname in the prompt.  This became 
exceedingly apparent at my last job with a bunch of junior admins, too 
many of which (read: > 0), inadvertently issued commands as the wrong 
user and / or on the wrong host.

I personally like having the current working directory in the prompt.

After some other incidents at my previous job, I started advocating for 
having the leading character in the prompt be a "#" so that most shells 
would not interpret it as a command pasted with trailing carriage return.

This brings me to the quintessential Bash / Zsh PS1 prompt of:

    PS1="#[\u@\h:\w]\$ "

The square brackets are effectively eye candy.  The \$ evaluates to 
indication of if you're running as a normal user or root.  It's largely 
superfluous with \u in the prompt.  But it makes things uniform and 
reduces questions when others are learning things.

When I migrated to Zsh, my prompt got a bit more complex.  The base 
prompt in a new terminal window still looks mostly the same as the above 
PS1.  The biggest difference is that I use Zsh's RPROMPT for transient 
things like the previous commands return code ($?) and date & time (the 
prompt was printed).  The RPROMPT has the nice feature of it leaves the 
screen when when the current command is executed, thus it doesn't 
clutter screen scroll back.

I'm leveraging Zsh's conditional prompt features to make the return code 
($?) stand out in bright white on bright red when $? != 0.

My shell initialization files conditionally set various environment 
variables which are used in the prompt.  Conditionally on things like:

  - the TERM type
  - my terminal emulator's answer back - used for terminal specific features
  - shell level
  - if the shell is forked from inside of screen / tmux
  - if the shell is forked from inside of vim

Depending on the TERM type and my terminal's answer back I'll either 
include control sequences to set the window title to (simplified version 
of) the PS1 prompt.  This way I have a good idea from the window title 
when selecting minimized windows from lists, where the terminal therein is.

I'm leveraging Zsh's preexec() function to conditionally set the window 
title to the command that's being executed if answer back indicates a 
supported terminal emulator.

I'm comparing the host name to the value of answer back to set the 
prompt color as a simple indicator if I'm on my local system or not.

  - Green  = terminal to local system
  - Yellow = terminal to a non-local system
  - Red    = terminal as root window somewhere, BE CAREFUL

So ... my Zsh prompt has a fair bit behind it.  But the bulk of what's 
printed is still the comment character, the username, the hostname, the 
current working directory, and the (non)root indicator.

One deviation is that I now have my root prompt (via sudo / su) slightly 
different in that after the effective user name, root, the prompt also 
includes my real user name in parenthesis.

    #[root(gtaylor)@host:~]#

As I do more with containers, I'm getting ready to implement a similar 
permutation on the host portion such that the host name portion reflects 
the container's / network namespace's value of \h with parent host OS's 
name in side of parenthesis, much like the effective / logged in user pair.

    #[root(gtaylor)@netns(host):~]#

Post-Script:  Happy holidays.  Here's hoping that things are going well 
for you and yours.



-- 
Grant. . . .
unix || die

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4017 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://minnie.tuhs.org/pipermail/coff/attachments/20211224/68259506/attachment.bin>


More information about the COFF mailing list