SSH, Forgetfulness, and PS1

SlowCoder

Gold Member
Gold Supporter
Joined
May 2, 2022
Messages
455
Reaction score
316
Credits
3,611
I like to use a rather minimal PS1 prompt. I usually know who I am, and where I am, so there's no sense including \u or \h. The PS1 on all my systems is:
Code:
PS1="${fgYellow}${italic}\w${fgWhite} \$ ${noColor}"

What happens when I SSH to another PC? Well, visual markers for who and where I am are minimal. There's been a few times I've forgotten where I was and modified the wrong this or that on the wrong PC.

What was my fix? I found the $SSH_CONNECTION variable that only exists inside an SSH session. So I leveraged it, and added the following code to my .bashrc:
Code:
# if terminal is SSH, add user/host
if [[ -n $SSH_CONNECTION ]]
then
    PS1="< ${fgLightRed}SSH:\u@\h${noColor} >\n$PS1"
fi

Note: Unrelated, but needed for colorization, I also have color definitions in my .bashrc file. Some folks might also find them useful. ;)

This is the end result, and makes my SSH prompts much more readable, while still allowing me to stay minimal:
Code:
# foreground colors
    fgBlack="\[\e[0;30m\]"
    fgRed="\[\e[0;31m\]"
    fgGreen="\[\e[0;32m\]"
    fgBrown="\[\e[0;33m\]"
    fgBlue="\[\e[0;34m\]"
    fgPurple="\[\e[0;35m\]"
    fgCyan="\[\e[0;36m\]"
    fgGray="\[\e[0;37m\]"
    fgDarkGray="\[\e[1;30m\]"
    fgLightRed="\[\e[1;31m\]"
    fgLightGreen="\[\e[1;32m\]"
    fgYellow="\[\e[1;33m\]"
    fgLightBlue="\[\e[1;34m\]"
    fgLightPurple="\[\e[1;35m\]"
    fgLightCyan="\[\e[1;36m\]"
    fgWhite="\[\e[1;37m\]"
# background colors
    bgBlack="\[\e[40m\]"
    bgRed="\[\e[41m\]"
    bgGreen="\[\e[42m\]"
    bgBrown="\[\e[43m\]"
    bgBlue="\[\e[44m\]"
    bgPurple="\[\e[45m\]"
    bgCyan="\[\e[46m\]"
    bgGray="\[\e[47m\]"
    bgDarkGray="\[\e[100m\]"
    bgLightRed="\[\e[101m\]"
    bgLightGreen="\[\e[102m\]"
    bgYellow="\[\e[103m\]"
    bgLightBlue="\[\e[104m\]"
    bgLightPurple="\[\e[105m\]"
    bgLightCyan="\[\e[106m\]"
    bgWhite="\[\e[107m\]"
# color reset
    noColor="\[\e[0m\]" # reset to default
# styles
    italic="\[\e[3m\]"
# other
    clearLineToEnd="\[\e[K\]" # clear line to end, with current color

PS1="${fgYellow}${italic}\w${fgWhite} \$ ${noColor}"

# if terminal is SSH, add user/host
if [[ -n $SSH_CONNECTION ]]
then
    PS1="< ${fgLightRed}SSH:\u@\h${noColor} >\n$PS1"
fi

Example Output:
Code:
< SSH:SlowCoder@Zeus >
~/Documents $
 
Top