[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Zoom.



Greg Wooledge wrote: 
> 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....


Regain some SAN points: none of the rest of it is weird. 

Two lines. First line is a colorized \u@\h:\w\, the second line
is the exit status and \$  


> 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:~$ 

Thanks, When I get around to redoing my PS1, I'll use that and
fix the quoting.

-dsr-


Reply to: