bash提示:如何只显示带PS1的文件名?

bash prompt : how to display only the filename with PS1?

我目前正在使用 PS1:

显示目录路径和 git 分支
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
export PS1="\u@\h \[3[32m\]\w\[3[33m\]$(parse_git_branch)\[3[00m\] $ "

这是提示:

john@myMac /Volumes/.../.../.../MyProject (master) $

但由于我的完整路径 ( \w\[3[33m\] 显示的很长,我想只显示文件名...

john@myMac MyProject (master) $

PS1中没有这样的选项...可能吗?

感谢反馈

删除 $PWD:

export PS1="\u@\h \[3[32m\]${PWD##*/}\[3[33m\]$(parse_git_branch)\[3[00m\] $ "

您可以使用 \W 而不是 \w:

export PS1="\u@\h \[3[32m\]\W\[3[33m\]$(parse_git_branch)\[3[00m\] $ "

根据man bash

\w     the  current working directory, with $HOME abbreviated with a tilde 
       (uses the value of the PROMPT_DIRTRIM variable)
\W     the basename of the current working directory, with $HOME abbreviated with a tilde