Gentoo Archives: gentoo-user

From: Frank Steinmetzger <Warp_7@×××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Bash prompt colours
Date: Mon, 06 Dec 2021 23:03:47
Message-Id: Ya6Wv+SZNevI5RDj@kern
In Reply to: Re: [gentoo-user] Bash prompt colours by Peter Humphrey
1 Am Fri, Dec 03, 2021 at 02:43:54PM +0000 schrieb Peter Humphrey:
2
3 > > > > Hello list,
4 > > > >
5 > > > > Is there a way to set the colour of a bash prompt according to
6 > > > > whether the user has SSH'd in?
7 > > > > […]
8 > > > When you are connected via SSH, the environment variable
9 > > > SSH_CONNECTION is set. I store the color in a variable and set it to
10 > > > yellow if `[[ -n "${SSH_CONNECTION}" ]]`. I can't give you the exact
11 > > > snippet since I use Zsh, but it should be possible to use a variable
12 > > > as color in bash's prompt?
13 > > >
14 > > > Kind regards, tastytea
15 > >
16 > > This link expands upon tastytea's idea:
17 > >
18 > > https://unix.stackexchange.com/questions/217270/change-ps1-color-when-> connected-to-other-host-via-ssh
19 >
20 > Thank you both. Now I just have to shoehorn it into /etc/bash/bashrc on the
21 > SSH server...
22
23 I expanded on that idea somewhat further. When I am in midnight commander
24 and press Ctrl+O to open a fullscreen shell, I sometimes forget after a
25 while that it was an mc shell, and so blindly quit it with ctrl+D, which
26 also kills the mc process. So a long time ago I expanded my PS1 in ~/.bashrc
27 for those shells to show the number of backgrounded processes and “subshell
28 type” like so:
29
30
31 __jobsprompt() {
32 PS1_JOBS_COUNT=`jobs -p | wc -l`
33 if [ $PS1_JOBS_COUNT -eq 0 ]; then
34 PS1_JOBS_COUNT=
35 else
36 PS1_JOBS_COUNT="$PS1_JOBS_COUNT "
37 fi
38 [ "$MC_SID" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}MC "
39 [ "$RANGER_LEVEL" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}R "
40 }
41
42 if [[ -z "$PROMPT_COMMAND" ]]; then
43 PROMPT_COMMAND=__jobsprompt
44 else
45 PROMPT_COMMAND="$PROMPT_COMMAND ; __jobsprompt"
46 fi
47
48 PS1="$CBPURPLE\u$CDGRAY@$CBGREEN\h$CRESET $CBBLUE\w $CRED"'$PS1_JOBS_COUNT'$CRESET"
49
50
51 Those $C… strings of course being shell codes for colours (CB…=bold). Note
52 the single quotes within the string. PS1 shall contain the actual string
53 '$PS1_JOBS_COUNT', so that the variable is expanded anew in every prompt.
54
55 --
56 Grüße | Greetings | Qapla’
57 Please do not share anything from, with or about me on any social network.
58
59 I just took an IQ test. The results were negative.

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies

Subject Author
Re: [gentoo-user] Bash prompt colours Grant Taylor <gtaylor@×××××××××××××××××××××.net>