Le Sat, Aug 08, 2026 at 10:17:17AM -0400, Greg Wooledge a écrit :
> It looks like your goal is to colorize a part of the prompt based on
> the current value of $?.
Yes, AND to add a job counter, but only when some background jobs does exist.
> This is very common, and the easiest way to do it is with an array.
> hobbit:~$ norm=$(tput sgr0) red=$(tput setaf 1)
> hobbit:~$ ps1colors=("$norm" "$red")
> hobbit:~$ PS1='\h:\w\[${ps1colors[!!$?]}\]\$\[$norm\] '
> hobbit:~$ false
> hobbit:~$ true
> hobbit:~$
>
> You can't see it in this email, but the $ becomes red after executing
> false.
>
> The array indexing uses !!$? which is a shorthand for flipping the
> "truthiness" value of $? twice. If $? is currently 0, flipping it
> twice goes 0 -> 1 -> 0. If it's nonzero, it goes x -> 0 -> 1.
> So either way, the index is always either 0 or 1, so the array only
> needs two values.
Hmm. For this, I would use:
$ PS1='\h:\w\[${ps1colors[$?>0]}\]\$\[$norm\] '
or
$ PS1='\h:\w\[${ps1colors[$?!=0]}\]\$\[$norm\] '
or even
$ PS1='\h:\w\[${ps1colors[$?==0?0:1]}\]\$\[$norm\] '
Seem less confusing.
--
Félix Hauri - <felix@f-hauri.ch> - http://www.f-hauri.ch