Welcome back, let’s discuss something that many UNIX/Linux users do not think about it even after they’ve been working on systems for years. Let’s discuss the bash prompt. If you don’t know what the bash prompt is, the bash prompt is the text that appears on the command line right before the cursor. Most Linux systems have a default bash prompt something like:
root@localhost:~#
The above prompt displays the user, the system name, and current directory and a $ or # depending if you are a regular or superuser.
Personally, I prefer something more descriptive and dynamic which also includes color, yes colors. There is a user variable called PS1 which can override the default prompt to whatever you’d like. The perfect prompt really depends on what you like to see every time you press Enter. My prompt is pretty complex, it also lists the git branch and checkout status if you are part of the git tree, but here it is:
export BLUE_B=12
export CYAN_B=14
export GRAY_B=8
export GREEN_B=10
export MAGENTA_B=13`export XIT=$? \ && git config –get user.name >/dev/null 2>&1 && git rev-parse –is-inside-work-tree >/dev/null 2>&1 && echo -n “\[$(tput setaf ${BLUE_B})\]@$(git config –get user.name):\[$(tput setaf ${GREEN_B})\]\h ” || echo -n “\[$(tput setaf ${BLUE_B})\]\u:\[$(tput setaf ${GREEN_B})\]\h ” \ && [ “$XIT” -ne “0” ] && echo -n “\[$(tput setaf ${RED_B})\]➜” || echo -n “\[$(tput setaf ${WHITE_B})\]➜”` \[\]\w `\ export BRANCH=$(git rev-parse –abbrev-ref HEAD 2>/dev/null); \ if [ “${BRANCH}” = “HEAD” ]; then \ export BRANCH=$(git describe –contains –all HEAD 2>/dev/null); \ fi; \ if [ “${BRANCH}” != “” ]; then \ echo -n “\[$(tput setaf ${CYAN_B})\](\[$(tput setaf ${RED_B})\]${BRANCH}” \ && if git ls-files –error-unmatch -m –directory –no-empty-directory -o –exclude-standard “:/*” > /dev/null 2>&1; then \ echo -n ” \[$(tput setaf ${YELLOW_B})\]✗”; \ fi \ && echo -n “\[$(tput setaf ${CYAN_B})\]) “; \ fi`\[\]\$ \[\]
Save the above content into a file called setupprompt.sh, give 755 mod, and source from the existing prompt.
Should you want to start and understand the prompt in more details, there are sites that help you build the PS1 variable. I will list some here for reference:
- https://bash-prompt-generator.org/
- https://robotmoon.com/bash-prompt-generator/
- https://ezprompt.net/
Have fun setting your prompt and type away…