Here's a modern and readable bash prompt script with detailed comments:
#!/bin/bash
# Modern Bash Prompt Configuration
# Add this to your ~/.bashrc or ~/.bash_profile
# Color definitions using ANSI escape codes
# These are wrapped in \[ \] to prevent issues with line wrapping
readonly COLOR_RESET='\[\033[0m\]'
readonly COLOR_BOLD='\[\033[1m\]'
readonly COLOR_DIM='\[\033[2m\]'
# Regular colors
readonly COLOR_RED='\[\033[0;31m\]'
readonly COLOR_GREEN='\[\033[0;32m\]'
readonly COLOR_YELLOW='\[\033[0;33m\]'
readonly COLOR_BLUE='\[\033[0;34m\]'
readonly COLOR_PURPLE='\[\033[0;35m\]'
readonly COLOR_CYAN='\[\033[0;36m\]'
readonly COLOR_WHITE='\[\033[0;37m\]'
readonly COLOR_GRAY='\[\033[0;90m\]'
# Bright colors
readonly COLOR_BRIGHT_RED='\[\033[0;91m\]'
readonly COLOR_BRIGHT_GREEN='\[\033[0;92m\]'
readonly COLOR_BRIGHT_YELLOW='\[\033[0;93m\]'
readonly COLOR_BRIGHT_BLUE='\[\033[0;94m\]'
# Unicode symbols for better visual appeal
readonly SYMBOL_BRANCH=""
readonly SYMBOL_ARROW="❯"
readonly SYMBOL_DIRTY="✗"
readonly SYMBOL_CLEAN="✓"
readonly SYMBOL_FOLDER=""
readonly SYMBOL_HOME=""
# Function to get git branch and status
git_prompt_info() {
# Check if we're in a git repository
if git rev-parse --git-dir > /dev/null 2>&1; then
# Get the current branch name
local branch=$(git branch --show-current 2>/dev/null)
# If no branch name (detached HEAD), get the short SHA
if [[ -z "$branch" ]]; then
branch=$(git rev-parse --short HEAD 2>/dev/null)
fi
# Check if there are uncommitted changes
if [[ -n $(git status --porcelain 2>/dev/null) ]]; then
echo " ${COLOR_YELLOW}${SYMBOL_BRANCH} ${branch} ${COLOR_RED}${SYMBOL_DIRTY}${COLOR_RESET}"
else
echo " ${COLOR_GREEN}${SYMBOL_BRANCH} ${branch} ${COLOR_GREEN}${SYMBOL_CLEAN}${COLOR_RESET}"
fi
fi
}
# Function to get Python virtual environment info
virtualenv_prompt_info() {
if [[ -n "$VIRTUAL_ENV" ]]; then
# Extract just the environment name from the full path
local env_name=$(basename "$VIRTUAL_ENV")
echo "${COLOR_PURPLE}(${env_name})${COLOR_RESET} "
fi
}
# Function to truncate long paths
truncate_pwd() {
local pwd_length=30 # Maximum length before truncation
local pwd_symbol="..."
# Replace home directory with ~
local current_pwd="${PWD/#$HOME/~}"
if [[ ${#current_pwd} -gt $pwd_length ]]; then
# Keep the last part of the path
local offset=$((${#current_pwd} - $pwd_length + ${#pwd_symbol}))
echo "${pwd_symbol}${current_pwd:$offset}"
else
echo "$current_pwd"
fi
}
# Function to display exit status of last command
exit_status_prompt() {
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
echo "${COLOR_RED}[${exit_code}]${COLOR_RESET} "
fi
}
# Function to build the complete prompt
build_prompt() {
# Save the exit status immediately
local last_exit=$?
# Start building the prompt
PS1=""
# Add newline for breathing room
PS1+="\n"
# Add virtual environment info if applicable
PS1+="$(virtualenv_prompt_info)"
# Add exit status if last command failed
if [[ $last_exit -ne 0 ]]; then
PS1+="${COLOR_RED}[${last_exit}]${COLOR_RESET} "
fi
# Add username with color based on privileges
if [[ $EUID -eq 0 ]]; then
# Root user - show in red
PS1+="${COLOR_RED}\u${COLOR_RESET}"
else
# Regular user - show in cyan
PS1+="${COLOR_CYAN}\u${COLOR_RESET}"
fi
# Add @ symbol
PS1+="${COLOR_GRAY}@${COLOR_RESET}"
# Add hostname in green
PS1+="${COLOR_GREEN}\h${COLOR_RESET}"
# Add separator
PS1+="${COLOR_GRAY}:${COLOR_RESET}"
# Add current directory in blue
PS1+="${COLOR_BLUE}$(truncate_pwd)${COLOR_RESET}"
# Add git information
PS1+="$(git_prompt_info)"
# Add newline and prompt symbol
PS1+="\n"
# Color the prompt symbol based on exit status
if [[ $last_exit -eq 0 ]]; then
PS1+="${COLOR_BRIGHT_GREEN}${SYMBOL_ARROW}${COLOR_RESET} "
else
PS1+="${COLOR_BRIGHT_RED}${SYMBOL_ARROW}${COLOR_RESET} "
fi
}
# Set the PROMPT_COMMAND to rebuild prompt before each command
PROMPT_COMMAND='build_prompt'
# Optional: Set terminal title to show current directory
case "$TERM" in
xterm*|rxvt*|screen*)
# Update terminal title with current directory
PROMPT_COMMAND="$PROMPT_COMMAND"'; echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/#$HOME/~}\007"'
;;
esac
# Enable color support for ls and grep
if [[ -x /usr/bin/dircolors ]]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# Additional useful aliases for a modern shell experience
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
This script provides:
- Color-coded elements - Username, hostname, directory, and git status all have distinct colors
- Git integration - Shows current branch and whether there are uncommitted changes
- Exit status indicator - Shows the exit code if the last command failed
- Virtual environment support - Displays Python virtual environment name when active
- Path truncation - Prevents extremely long paths from making the prompt unwieldy
- Root user warning - Username appears in red when running as root
- Dynamic prompt symbol - Changes color based on the success/failure of the last command
- Terminal title updates - Sets the terminal window title to show current directory
To use this script, save it to a file (e.g., ~/.bash_prompt) and source it from your ~/.bashrc:
# Add to ~/.bashrc
if [[ -f ~/.bash_prompt ]]; then
source ~/.bash_prompt
fi
The prompt will look something like:
username@hostname:~/projects/myapp main ✓
❯
Or with a failed command and uncommitted changes:
[127] username@hostname:~/projects/myapp main ✗
❯