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: Félix Hauri
Subject: Re: realine messup with dynamic PS1
Date: Sat, 8 Aug 2026 15:26:36 +0200

Le Fri, Aug 07, 2026 at 12:12:13PM -0400, Chet Ramey a écrit :
> 
> So what does this actually produce? What's the value of PS1?

Reading this, I've tried to simplify:

PS1="\[\e[0;32m\]\u\[\e[0;2;37m\]@\[\e[0;32m\]\h\[\e[0;2;37m\]:\[\e[0;1;34m\]\w$(
    rCod=$?
    [ -n "$(jobs -s; jobs -r)" ] &&
        echo -n '\[\e[0;2;37m\]|\[\e[0;36m\]\j'
    [ "$rCod" -eq 0 ] &&
        echo -n '\[\e[0;2;37m\]' || echo -n '\[\e[0;31m\]'
    exit $rCod
)\$\[\e[0m\e]0;\u@\h:\w (\j)\a\] "

Note:
 - there are only one inline command `$(...)` instead of two
 - the use of a variable to store original Result Code (in order to
      drive them correctly)
 - the use of a single bar `|\j` instead of braces `{\j}`

Into a function to be assigned to PROMPT_COMMAND:

rePs1() {
    
PS1="\[\e[0;32m\]\u\[\e[0;2;37m\]@\[\e[0;32m\]\h\[\e[0;2;37m\]:\[\e[0;1;34m\]\w$(
        rCod=$?
        [ -n "$(jobs -s; jobs -r)" ] &&
            echo -n '\[\e[0;2;37m\]|\[\e[0;36m\]\j'
        [ "$rCod" -eq 0 ] &&
            echo -n '\[\e[0;2;37m\]' || echo -n '\[\e[0;31m\]'
        exit $rCod
    )\$\[\e[0m\e]0;\u@\h:\w (\j)\a\] "
}
PROMPT_COMMAND=rePs1

In the hope this help to understand original goal of this.
(Hopping I've correctly understood too!)

But personally, trying to avoid forks, I will write this `rsPs1` as:

rePs1() {
    local rCod=$? jPs='' dollarColor
    [[ $(jobs -s; jobs -r) ]] && jPs='\[\e[0;2;37m\]|\[\e[0;36m\]\j'
    ((rCod)) && dollarColor='\[\e[0;31m\]' || dollarColor='\[\e[0;2;37m\]'
    PS1='\[\e[0;32m\]\u\[\e[0;2;37m\]@\[\e[0;32m\]\h\[\e[0;2;37m\]:'
    PS1+="\[\e[0;1;34m\]\w$jPs$dollarColor\$\[\e[0m\e]0;\u@\h:\w (\j)\a\] "
    return $rCod
}

Or the same, using `\1` instead of `\[` and `\2` in place of `\]`:

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!

-- 
 Félix Hauri  -  <felix@f-hauri.ch>  -  http://www.f-hauri.ch



reply via email to

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