I’m looking for inspiration for a custom Bash prompt[1]. I’d love to see yours! 😊 If possible, include both the prompt’s PS1
, and a screenshot/example of what it looks like.
References
- Type: Documentation. Title: “Bash Reference Manual”. Publisher: Gnu Project. Edition: 5.2. Published: 2022-09-19. Accessed: 2025-03-21T02:46Z. URI: https://www.gnu.org/software/bash/manual/html_node/index.html.
- §6.9 “Controlling the Prompt”. URI: https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html.
Crossposts:
Mine shows the user and host, git commit and branch, docker context and directory, color coded based on status of git:
[root@server001|G:19e526e@(master)|D:myContext|currentDir] $
## PS1 adapted from https://gist.github.com/xenji/2292341 ps1_generator() { # docker context inspect --format '{{ .Name }}' Time12h="\T"; Time12a="\@"; ShortHost="\h"; Username="\u"; PathShort="\W"; PathFull="\w"; NewLine="\n"; Jobs="\j"; test -f ~/.config/git-prompt.sh || \ curl -L https://raw.github.com/git/git/master/contrib/compl etion/git-prompt.sh \ > ~/.config/git-prompt.sh source ~/.config/git-prompt.sh Color_Off="\[\033[0m\]"; IBlack="\[\033[0;90m\]"; BWhite="\[\03 3[1;37m\]"; BGreen="\[\033[1;32m\]"; BIRed="\[\033[1;91m\]"; BIWhite="\[\033[1;97m\]"; BIPurple="\[\ 033[1;95m\]"; BIBlue="\[\033[1;94m\]"; GIT_PS1='$(git branch &>/dev/null;\ if [ $? -eq 0 ]; then \ echo "$(echo `git status` | \grep "nothing to commit" > /dev/null 2>&1; \ DIRTY="$?"; \ HEADREV=`git log --pretty=%h -n 1`; \ echo -n "|G:'${BWhite}'$HEADREV"; \ if [ "$DIRTY" -eq "0" ]; then \ # @4 - Clean repository - nothing to commit echo "@'${BGreen}'"$(__git_ps1 "(%s)"); \ else \ # @5 - Changes to working tree echo "'${BIBlue}'@'${BIRed}'"$(__git_ps1 "{%s}"); \ fi)'${Color_Off}'"; \ else \ # @2 - Prompt when not in GIT repo echo ""; \ fi)' if docker context inspect >/dev/null 2>&1; then DOCKER_PS1='|D:'${BIBlue}'$(docker context inspect --format "{{ .Name }}")'${Color_Off} fi USER_PS1=${BIPurple}${Username}'@'${ShortHost}${Color_Off} PATH_PS1='|'${BWhite}${PathShort}${Color_Off} export PS1='['${USER_PS1}${GIT_PS1}${DOCKER_PS1}${PATH_PS1}'] $ ' } ps1_generator && unset -f ps1_generator
Are you dowloading and sourcing a file from the internet on your prompt? That sounds a bit scary!
Well, yeah, but it’s git: https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh
My thinking is that I trust git on my computer, so I trust downloading from their repo.
But you’re right. I should revisit this and see if it’s even necessary.
Is this running in your rc (i.e. every single time you open a terminal)? Even if it’s safe, I’d be annoyed by any delay.
Yeah, its checking if the file exists first, so it’s not doing it all the time.
My worry is more related to repos takeovers or hacks. This is pretty hidden, so it could be easy to even forget it’s there, probably not the worst, but still…
It is in my .bashrc, but any delay is not noticeable.
Ah that’s fair, I didn’t look closely