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.
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
On Friday, 3 December 2021 12:08:05 GMT tastytea wrote:wrote:
On 2021-12-03 11:17+0000 Peter Humphrey <peter@prh.myzen.co.uk>
SSH_CONNECTIONHello 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
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
Hello list,
Is there a way to set the colour of a bash prompt according toWhen you are connected via SSH, the environment variable
whether the user has SSH'd in?
[…]
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...
[ "$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
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}
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 286 |
Nodes: | 16 (2 / 14) |
Uptime: | 87:57:35 |
Calls: | 6,496 |
Calls today: | 7 |
Files: | 12,100 |
Messages: | 5,277,265 |