bash提示:如何仅使用PS1显示文件名?

时间:2017-10-30 10:59:29

标签: bash sed ps1

我目前正在使用PS1显示目录路径和git分支:

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

提示:

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

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

john@myMac MyProject (master) $

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

感谢您的反馈

2 个答案:

答案 0 :(得分:1)

您可以使用\W代替\w

export PS1="\u@\h \[\033[32m\]\W\[\033[33m\]\$(parse_git_branch)\[\033[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

答案 1 :(得分:0)

削减$ PWD:

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