下划线 bash 仅在 pwd 更改时提示

Underlining to bash prompt only when pwd changed

我想在我的 bash shell 提示符 (=PS1) 下划线,只有当当前目录被更改时。 我试过了。

.bashrc 我写的文件

DIR_CHANGED=
function cd {
    builtin cd "$@" 
    DIR_CHANGED=1
}

function dir_ul {
    # if $DIR_CHANGED is 1, draw underline
    if [ x == x$DIR_CHANGED ]; then echo -en '3[0;34m'; else echo -en '3[4;34m'; fi  
    export DIR_CHANGED=''
}

export PS1='$(dir_ul)\w$(tput sgr0)$ '

但没有成功。

我该如何解决?

在你的 ~/.bashrc 中再测试一下 shell:

PREV="$PWD"
PROMPT_COMMAND='[[ $PREV != $PWD ]] && PS1="$(tput smul)\w$(tput rmul)$ " && PREV="$PWD" || PS1="$(tput rmul)\w$ "'

$(dir_ul) 会在您设置 PS1 变量时进行评估,而不是持续更新。

我不想这么说,但我认为这是一个 bash 错误。这是一个解决方法:使用 PROMPT_COMMAND 来 复制 $DIR_CHANGED 并重置它,并在 dir_ul 中引用该保存的副本。最小变化:

function dir_ul {
    if [ x == x$DIR2 ]; then echo -en '3[0;34m'; else echo -en '3[4;34m'; fi  
}
PROMPT_COMMAND='DIR2=$DIR_CHANGED;DIR_CHANGED='