博主头像
mxd's Blog

以技术为翼,以生活为魂

分享下我的Bash PS1

看到了 wyf9's Blog - Bash / CMD 提示符分享,那么我也来分享个我自己的

效果预览:

share-bash-ps1-01_01.webp
share-bash-ps1-01_01.webp

# Git 分支和脏状态检测函数
parse_git_branch() {
  if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    local branch=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
    local status=""
    # 检查未暂存的修改 (*)
    if [ -n "$(git status --porcelain | grep -E '^( M|\?\?)')" ]; then
      status="${status}*"
    fi
    # 检查已暂存但未提交的更改 (+)
    if [ -n "$(git status --porcelain | grep -E '^(M |A )')" ]; then
      status="${status}+"
    fi
    echo "[git:${branch}${status}]"
  else
    echo ""
  fi
}

# 自定义
PS1='\[\e[1;32m\]\u@\h\[\e[0m\] \[\e[1;34m\]\w\[\e[0m\] \[\e[1;35m\]$(parse_git_branch)\[\e[0m\] \[\e[1;31m\]$(LC_ALL= LC_TIME=en_US.UTF-8 date +%y/%m/%d\ %H:%M\ %a)\[\e[0m\]\n\[\e[1;33m\]\$\[\e[0m\] '

# alias
alias lla='ls -la' # 详细列表

# Bash completion
[[ $PS1 && -f /usr/local/share/bash-completion/bash_completion.sh ]] && \
        source /usr/local/share/bash-completion/bash_completion.sh
# Debian
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

注意下,我当前系统是 FreeBSD,所以 Bash 补全的脚本在 /usr/local/share/bash-completion/bash_completion.sh,如果你是 Linux,请替换为你的发行版所在位置,不懂请 Google。

Debian 在 /usr/share/bash-completion/bash_completion。需要提前 sudo apt install bash-completion

分享下我的Bash PS1
https://blog.mxdyeah.com/post/share-bash-ps1-01
本文作者 mxdyeah
发布时间 2025-11-26
许可协议 CC BY-NC-SA 4.0
发表新评论

AD: