Am Fri, Dec 03, 2021 at 02:43:54PM +0000 schrieb Peter Humphrey:

> > > > Hello list,
> > > > 
> > > > Is there a way to set the colour of a bash prompt according to
> > > > whether the user has SSH'd in?
> > > > […]
> > > When you are connected via SSH, the environment variable
> > > SSH_CONNECTION is set. I store the color in a variable and set it to
> > > yellow if `[[ -n "${SSH_CONNECTION}" ]]`. I can't give you the exact
> > > snippet since I use Zsh, but it should be possible to use a variable
> > > as color in bash's prompt?
> > > 
> > > Kind regards, tastytea
> > 
> > This link expands upon tastytea's idea:
> > 
> > https://unix.stackexchange.com/questions/217270/change-ps1-color-when-> 
> > connected-to-other-host-via-ssh
> 
> Thank you both. Now I just have to shoehorn it into /etc/bash/bashrc on the 
> SSH server...

I expanded on that idea somewhat further. When I am in midnight commander
and press Ctrl+O to open a fullscreen shell, I sometimes forget after a
while that it was an mc shell, and so blindly quit it with ctrl+D, which
also kills the mc process. So a long time ago I expanded my PS1 in ~/.bashrc
for those shells to show the number of backgrounded processes and “subshell
type” like so:


__jobsprompt() {
    PS1_JOBS_COUNT=`jobs -p | wc -l`
    if [ $PS1_JOBS_COUNT -eq 0 ]; then
        PS1_JOBS_COUNT=
    else
        PS1_JOBS_COUNT="$PS1_JOBS_COUNT "
    fi
    [ "$MC_SID" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}MC "
    [ "$RANGER_LEVEL" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}R "
}

if [[ -z "$PROMPT_COMMAND" ]]; then
    PROMPT_COMMAND=__jobsprompt
else
    PROMPT_COMMAND="$PROMPT_COMMAND ; __jobsprompt"
fi

PS1="$CBPURPLE\u$CDGRAY@$CBGREEN\h$CRESET $CBBLUE\w 
$CRED"'$PS1_JOBS_COUNT'$CRESET"


Those $C… strings of course being shell codes for colours (CB…=bold). Note
the single quotes within the string. PS1 shall contain the actual string
'$PS1_JOBS_COUNT', so that the variable is expanded anew in every prompt.

-- 
Grüße | Greetings | Qapla’
Please do not share anything from, with or about me on any social network.

I just took an IQ test. The results were negative.

Attachment: signature.asc
Description: PGP signature

Reply via email to