Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #228016

Re: Zoom.

From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups linux.debian.user
Subject Re: Zoom.
Date 2020-10-20 13:40 +0200
Message-ID <B1Y9I-8vu-15@gated-at.bofh.it> (permalink)
References <B11CF-6Tb-1@gated-at.bofh.it> <B15Gh-N6-1@gated-at.bofh.it> <B1KzN-lB-29@gated-at.bofh.it> <B1Pzs-3kQ-1@gated-at.bofh.it> <B1XdE-7VW-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Tue, Oct 20, 2020 at 06:36:15AM -0400, Dan Ritter wrote:
> I have this in my PS1 definition:
> 
> \$(if [[ \$? == 0 ]]; then echo \"+\"; else echo \"-\"; fi)\
> 
> Which has the effect of telling me the rough exit status of the
> last command in my prompt. 

The weird backslashing tells me you're defining PS1 with double quotes
instead of single quotes, which is weird, and probably indicates that
you're doing even worse things, like dynamically generating pieces of
your PS1... I've seen things... things that no man should see....

Anyway, in addition to the weird quoting and possible abominations
unrevealed, your command substitution forks.  Every time the prompt
is drawn, a new process is forked, just to do that simple "if" check.

The standard workaround for that is to use an array variable with the
two possible outcomes, and a bit of arithmetic.

unicorn:~$ green=$(tput setaf 2) red=$(tput setaf 1) normal=$(tput sgr0)
unicorn:~$ status=(+ -) statcolor=("$green" "$red")
unicorn:~$ PS1='\[${statcolor[!!$?]}\]${status[!!$?]}\[$normal\] \h:\w\$ '
+ unicorn:~$ true
+ unicorn:~$ false
- unicorn:~$ true
+ unicorn:~$ 

(You can't see the colors in the mailing list, but they're there.)

Just remember the basics:

1) Use single quotes around your definition of PS1, because you want all
   of the parts of it to be taken literally, and quoting is already hard
   enough without making it worse.

2) Variable expansions are "free" (do not cost a process fork).  This
   includes variables that are set by PRMOPT_COMMAND, which is the more
   efficient way to set variables dynamically if you *really* need that,
   which you probably don't.

3) Color and other terminal escape codes can and should be stored in
   variables, which you can expand in PS1, as long as the PS1 definition
   is single-quoted.  Don't store raw terminal sequences, because they
   are not the same for all terminals.  This is what tput(1) is for.

4) Terminal escape sequences that don't change the cursor position should
   be enclosed in \[ \] so the shell knows where the cursor is at all
   times.

5) When expanding the index of an indexed array variable, the square
   brackets are a math context.  In a math context, !!$? means "negate
   the previous command's exit status twice".  So, 0 -> 1 -> 0, or
   nonzero -> 0 -> 1.  Thus, !!$? always expands to either 0 or 1.  And
   because it's in single quotes, it shouldn't trip history expansion,
   if you still have history expansion turned on.  (I don't.)

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Zoom. peter@easthope.ca - 2020-10-17 23:10 +0200
  Re: Zoom. Charles Curley <charlescurley@charlescurley.com> - 2020-10-17 23:40 +0200
  Re: Zoom. Peter Ehlert <peter@sdi-baja.com> - 2020-10-18 00:10 +0200
  Re: Zoom. Carl Fink <carlf@panix.com> - 2020-10-18 01:00 +0200
    Re: Zoom. conover@rahul.net (John Conover) - 2020-10-18 01:40 +0200
  Re: Zoom. David <bouncingcats@gmail.com> - 2020-10-18 01:50 +0200
    Re: Zoom. riveravaldez <riveravaldezmail@gmail.com> - 2020-10-18 05:10 +0200
    Re: Zoom. Tom Dial <tddial@comcast.net> - 2020-10-18 09:20 +0200
  Re: Zoom. Bob McGowan <ramjr0915@gmail.com> - 2020-10-18 03:30 +0200
    Re: Zoom. peter@easthope.ca - 2020-10-19 23:10 +0200
      Re: Zoom. Michael Stone <mstone@debian.org> - 2020-10-19 23:10 +0200
        Re: Zoom. peter@easthope.ca - 2020-10-22 15:30 +0200
          Re: Zoom. Michael Stone <mstone@debian.org> - 2020-10-22 16:40 +0200
      Re: Zoom. John Crawley <john@bunsenlabs.org> - 2020-10-20 04:30 +0200
        Re: Zoom. Dan Ritter <dsr@randomstring.org> - 2020-10-20 12:40 +0200
          Re: Zoom. Nicolas George <george@nsup.org> - 2020-10-20 13:20 +0200
          Re: Zoom. Greg Wooledge <wooledg@eeg.ccf.org> - 2020-10-20 13:40 +0200
            Re: Zoom. Dan Ritter <dsr@randomstring.org> - 2020-10-20 14:10 +0200
            Re: shells (was Re: Zoom.) rhkramer@gmail.com - 2020-10-20 17:00 +0200
              Re: shells (was Re: Zoom.) Nicolas George <george@nsup.org> - 2020-10-20 17:00 +0200
              Re: shells (was Re: Zoom.) Tony van der Hoff <lists@vanderhoff.org> - 2020-10-20 17:40 +0200
  Re: Zoom. A_Man_Without_Clue <love.chaser@gmail.com> - 2020-10-18 05:20 +0200
  Re: Zoom. Stefan Monnier <monnier@iro.umontreal.ca> - 2020-10-18 17:20 +0200
    Re: Zoom. Sven Hartge <sven@svenhartge.de> - 2020-10-18 17:50 +0200
      Re: Zoom. <tomas@tuxteam.de> - 2020-10-19 09:30 +0200
        Re: Zoom. Sven Hartge <sven@svenhartge.de> - 2020-10-19 09:40 +0200
          Re: Zoom. David Wright <deblis@lionunicorn.co.uk> - 2020-10-19 17:10 +0200
          Re: Zoom. Stefan Monnier <monnier@iro.umontreal.ca> - 2020-10-19 17:30 +0200
            Re: Zoom. <tomas@tuxteam.de> - 2020-10-19 17:40 +0200
            Re: Zoom. Stefan Monnier <monnier@iro.umontreal.ca> - 2020-10-19 18:00 +0200
          Re: Zoom. Stefan Monnier <monnier@iro.umontreal.ca> - 2020-10-19 17:40 +0200
        Re: Zoom. "Thomas Schmitt" <scdbackup@gmx.net> - 2020-10-19 10:00 +0200
    Re: Zoom. peter@easthope.ca - 2020-10-20 03:20 +0200
      Re: Zoom. <tomas@tuxteam.de> - 2020-10-20 10:10 +0200
    Re: Zoom. peter@easthope.ca - 2020-10-20 03:30 +0200
    Server for Jitsi; was: Zoom. peter@easthope.ca - 2020-10-22 20:20 +0200
      Re: Server for Jitsi; was: Zoom. Dan Ritter <dsr@randomstring.org> - 2020-10-22 20:40 +0200
      Re: Server for Jitsi; was: Zoom. "der.hans" <deb-user@LuftHans.com> - 2020-10-22 21:00 +0200
      Re: Server for Jitsi; was: Zoom. Stefan Monnier <monnier@iro.umontreal.ca> - 2020-10-23 01:40 +0200

csiph-web