• Re: [gentoo-user] Bash prompt colours

    From tastytea@21:1/5 to peter@prh.myzen.co.uk on Fri Dec 3 13:10:02 2021
    On 2021-12-03 11:17+0000 Peter Humphrey <peter@prh.myzen.co.uk> wrote:

    Hello list,

    Is there a way to set the colour of a bash prompt according to
    whether the user has SSH'd in?

    This machine is a compile host for some others on the LAN, and it
    would be helpful if it were more obvious that I'm connected to
    another machine. Of course, the standard prompt tells me the machine
    name, but something more conspicuous would help.

    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

    --
    Get my PGP key with `gpg --locate-keys tastytea@tastytea.de` or at <https://tastytea.de/tastytea.asc>.

    --Sig_/ifFcq0nGSxSmz7VN3_0H/9Content-Type: application/pgp-signature Content-Description: Digitale Signatur von OpenPGP

    -----BEGIN PGP SIGNATURE-----

    iHUEAREKAB0WIQQ1VSZoZMptf/RapufPw5SX8bJuBwUCYaoIpgAKCRDPw5SX8bJu BxcyAP4jB7HiVm8NNklgQ3pUkQHdILUiDUmsDOEzDNeMZFCUkQD7BXIm6zQSzQQ/ xbeKnrP0diABYku+hhJjo3nuM4ugoYo=K238
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Humphrey@21:1/5 to All on Fri Dec 3 12:20:02 2021
    Hello list,

    Is there a way to set the colour of a bash prompt according to whether the
    user has SSH'd in?

    This machine is a compile host for some others on the LAN, and it would be helpful if it were more obvious that I'm connected to another machine. Of course, the standard prompt tells me the machine name, but something more conspicuous would help.

    --
    Regards,
    Peter.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael@21:1/5 to All on Fri Dec 3 13:30:29 2021
    On Friday, 3 December 2021 12:08:05 GMT tastytea wrote:
    On 2021-12-03 11:17+0000 Peter Humphrey <peter@prh.myzen.co.uk> wrote:
    Hello list,

    Is there a way to set the colour of a bash prompt according to
    whether the user has SSH'd in?

    This machine is a compile host for some others on the LAN, and it
    would be helpful if it were more obvious that I'm connected to
    another machine. Of course, the standard prompt tells me the machine
    name, but something more conspicuous would help.

    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

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCAAdFiEEXqhvaVh2ERicA8Ceseqq9sKVZxkFAmGqG/UACgkQseqq9sKV ZxkrohAA62GiI/a2HdP8npnJ5OsheP96m+QZSqkyv9iHEX3iSPAIfbl4XAWqeMrl 8HHU3zupABbLZcrNzh7Wqtg47aYOjTt115dLjJh2uMo+vBZ5cpofCJuXJ88P1XVU RUTHShujCK8DdhS8OMkS4AOyn1BEIJ9JVTxn/QnSnPM3x2X+gDSzpQqbM1TOIwHI uEHTIOl3DyHeJ7jVsAwdQ5u3R+tbSmDL5pZCvHo7EulwoF2a9ClSrb3KqfoUoZ8m dV2jXSCzzK/9tz29FCrGh/H7jG7H0su0eoLrEjFVz+FofDMaLg03KlRNLDVfKL9g /E5tPaFV0rv0uaG2ZAlrxOg7xG10R3JiWj7jdxwyBIjyI86ox2Aor0aor16tXIT8 6u/i9RtkxQ63W3fi4UPRFx/l2VGxtuKHHrjDkbnJbfEGUb/CKm69nmO8tSPFQkbW SRAc/UaXx6V4lR4yKH86g7A57ioYwvUe9SbEOpF2qtNEjpzi1ea/Wcmbv0eRmcXl cOBO1Ytm79/1jN3aka23GFKAqf6S2vU1l5AsxyIYOrfMj76qnEA3GxagX4n90EsM hV912RgwIwlRzDBqhlN6eYqmLiK2cDqmBLq9U1oH0o3MsYLWHwTXeQgITJ5FDUl6 YU5idFr1kRqtK4iQ5rncAuoxulmGeCily/BWv40PwfIC75EBY8c=
    =34Jq
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Humphrey@21:1/5 to All on Fri Dec 3 15:50:01 2021
    On Friday, 3 December 2021 13:30:29 GMT Michael wrote:
    On Friday, 3 December 2021 12:08:05 GMT tastytea wrote:
    On 2021-12-03 11:17+0000 Peter Humphrey <peter@prh.myzen.co.uk>
    wrote:
    Hello list,

    Is there a way to set the colour of a bash prompt according to
    whether the user has SSH'd in?

    This machine is a compile host for some others on the LAN, and it
    would be helpful if it were more obvious that I'm connected to
    another machine. Of course, the standard prompt tells me the machine name, but something more conspicuous would help.

    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...

    --
    Regards,
    Peter.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Frank Steinmetzger@21:1/5 to All on Tue Dec 7 00:10:01 2021
    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.

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEEVbE9o2D2lE5fhoVsizG+tUDUMMoFAmGulr0ACgkQizG+tUDU MMqB/w//XfLHgF8gXFG0I9Qi2iyifv1hYqvABJhFZZXlIWU0mmbRCXIvo03SYeLq lEhMYmDDDDRPOQiUnxkQopG16DPXECuxjALOTLAz8DfjFsoFUa6KUR85UYvdVOyI BMcKj7Igzdc0bEZqLHRiu22OA3Yp3PjeE/dZtxtG+40c/hqyFL9ItT91ak6O7o7b XORu0PD3t5gHtQG8qHVrMaojfy1ETNXL5fltaHB+YHZFlr/vZ1QQTos6mFOk0d0q cMkUlBGjOSOPODhM2sfRAlSE5LwDLOosYUUC2u5v5aTTm7q1fsjLlIp67LiPmHpe XQjcBlrp4LM8oDLIlwxE7UZAsLuzq/9qwe358NYWbblef4zIDb+MbM2ngKI7l/n3 SOsYVKvXEdWOP/9O1qdC3KVwVII4o+RjSO/bYZcCctKDu1sKsjBcWe2kf69WF3JY e3kKQBXJTnHmF43TG22RdmZ8bu11wE7QQVFFPOHNN7Gw82Ua1dyOdit2FidkpZOt vuvZKjwrRTrNVgmbrwSDe+Nclic6tRiUYqO57ljw5YV7K7GhEBebrqs7+m0CjhlX i6Dx9xydR3V8M0iNEb0e1QBiQvIw9IiY2fF9Idrn/dbVrg/6nvR/JXP6LWwwLdZc a3zp21LfQdhi//BEc7deJaMCtmZ0Zy0hYBEEMAcdWfcS+bLmrmU=
    =yRzA
    -----END PGP SIGNATURE-----

    ---
  • From Grant Taylor@21:1/5 to Frank Steinmetzger on Tue Dec 7 18:30:01 2021
    Some drive-by after-the-fact comments:

    On 12/6/21 4:03 PM, Frank Steinmetzger wrote:
    [ "$MC_SID" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}MC "
    [ "$RANGER_LEVEL" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}R "

    I've taken to using things like the following:

    PS1_JOBS_COUNT="${PS1_JOBS_COUNT}${MC_SID:+MC }${RANGER_LEVEL:+R }"

    Leverage Bash's (and Zsh's) expansion conditional. If the variable is
    set, then expand it to a different value.

    ${VARIABLE:+alternate text to show if VARIABLE is set}

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

    Is there a reason to not simply do the following, eliminating the if conditional:

    PROMPT_COMMAND=${PROMPT_COMMAND:+${PROMPT_COMMAND} ; __jobsprompt}



    --
    Grant. . . .
    unix || die

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Frank Steinmetzger@21:1/5 to All on Tue Dec 7 22:00:01 2021
    Am Tue, Dec 07, 2021 at 10:26:11AM -0700 schrieb Grant Taylor:
    Some drive-by after-the-fact comments:

    On 12/6/21 4:03 PM, Frank Steinmetzger wrote:
    [ "$MC_SID" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}MC "
    [ "$RANGER_LEVEL" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}R "

    I've taken to using things like the following:

    PS1_JOBS_COUNT="${PS1_JOBS_COUNT}${MC_SID:+MC }${RANGER_LEVEL:+R }"

    Leverage Bash's (and Zsh's) expansion conditional. If the variable is set, then expand it to a different value.

    By jove, you’re right. Maybe I’ve written that stuff before I knew about default values. I checked in that code in September of 2016. And it could possibly be even much older, because I clean up my config git repository
    only very sporadically, as in two or three times a year.

    ${VARIABLE:+alternate text to show if VARIABLE is set}

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

    Is there a reason to not simply do the following, eliminating the if conditional:

    PROMPT_COMMAND=${PROMPT_COMMAND:+${PROMPT_COMMAND} ; __jobsprompt}

    Maybe inexperience? :D
    Or sometimes, explicit is better than implicit?

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

    I had a problem and used Java. Now I have a ProblemFactory.

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEEVbE9o2D2lE5fhoVsizG+tUDUMMoFAmGvyucACgkQizG+tUDU MMqRXA/+K5miMzq8DzyOi/O3DBdaWja4aKs6RapCgFh5Ol6LB3pEUkreEY18EsoA ISI3W7KLWbUo7FQA/aWbf0t1COH8SIQuQrarM5N7pRCECkW7IuUFnfaRGIuOoSNb jIYUJw44l1C0L9zwFyM2GjcXNYZpO5o8NGF7AVnO3oOs5/gIEU9cYImEXPHndLOs uak7kkfweIcz4tmeB6vJlnOEOyBq1ZKmSF1m69y1r0Whl3243AJ86sKBZ4eW2KGh iMN4OGZoKe8zGfU8Pc9Yff/d7bRiDki/rjYjs6286RZQP7Ra4PzdK1CDvjKmMTlz 36p4kjBn5lSbHKs88eJGDS79yOQXe6Wi43fGCV5NuIu6G4KfVKqi0mHgBzmc5JP5 gDqS1X/Bqa5OYdJPa6GhUylffK2mlZ0gMnbZ5UP/mJWxbth5HvyWqJDd9VikF7Za pDgddZajkQqPyUnGyKYXfDFfyRPCuTu+LDeei3L1dovj/lu6s4lwAUV3QKl5LPjs FLTn3GH2NBaHq1kJgCCAll16CCs6PR9rg8HZRAa6c4cWJLJT4BqCJ+6mGdTVoGcF +vEvkYM9kqWBng2R6mPQoP8VZfKvA5jYs+XEoy3ztrL9HUBngVPlkaqNqMKHVHU0 C1Owi3GFh1Nik+XB+LuL8w3wRHkLJy91D9QkyX+iEF0EPusowD8=
    =TiEX
    -----END PGP SIGNATURE----