2 ms·
Here's what I'm currently rolling: $ A colorful prompt appears, only when 'user or hostname or path' changes, or after certain commands: - username@host
by mmphosis 5y ago
Here's what I'm currently rolling:
$
A colorful prompt appears, only when 'user or hostname or path' changes, or after certain commands:
- username@hostname:pwd
$
Somewhere in .bash_aliases ...
### PROMPT ###
### long prompt (in color) only when 'user or hostname or path' changes, or after certain commands
prompt_command ()
{
local -a a;
local last_cmd;
IFS=' ' read -r -d '' -a a < <( history 1 );
last_cmd="$(printf "%s" "${a[1]}"|xargs -0)";
if [ "$USER_HOSTNAME_PWD" != "$USER@$HOSTNAME:$PWD" ] || [[ " cd src doc ssh sudo su login $(sed -e '/^#.*$/d' -e '/^$/d' -e 's@^.*/@@' /etc/shells|uniq|xargs) " == *" ${last_cmd} "* ]]; then
USER_HOSTNAME_PWD="$USER@$HOSTNAME:$PWD";
echo -e "\\e7\\e(B\\e[m- \\e[3$((($(id -u) != 0) + 1))m${USER}\\e(B\\e[m@\\e[35m${HOSTNAME%%.*}\\e(B\\e[m:\\e[36m${PWD/#$HOME/\~}\\e8";
fi
}
-() { unset USER_HOSTNAME_PWD
}
PROMPT_COMMAND=prompt_command
PS1='$ ';[ "$(id -u)" -eq 0 ] && PS1='# '