help-bash
[Top][All Lists]
Advanced

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

Re: realine messup with dynamic PS1


From: Greg Wooledge
Subject: Re: realine messup with dynamic PS1
Date: Sat, 8 Aug 2026 10:17:17 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Sat, Aug 08, 2026 at 15:26:36 +0200, Félix Hauri via wrote:
> rePs1() {
>     local rCod=$? jPs='' dollarColor
>     [[ $(jobs -s; jobs -r) ]] && jPs=$'\1\\e[0;2;37m\2|\1\\e[0;36m\2\\j'
>     ((rCod)) && dollarColor=$'\1\\e[0;31m\2' || dollarColor=$'\1\\e[0;2;37m\2'
>     
> PS1=$'\1\\e[0;32m\2\\u\1\\e[0;2;37m\2@\1\\e[0;32m\2\\h\1\\e[0;2;37m\2:\1\\'
>     PS1+=$'e[0;1;34m\2\\w'$jPs$dollarColor$'\\$\1\\e[0m\e]0;\u@\h:\w (\j)\a\2 
> '
>     return $rCod
> }
> 
> Seem to work fine too!

It looks like your goal is to colorize a part of the prompt based on
the current value of $?.  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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]