gpt4 book ai didi

bash - PS1 定义中的条件密码

转载 作者:行者123 更新时间:2023-12-02 15:00:48 25 4
gpt4 key购买 nike

如果我在 symlink 中,我想在我的提示中以不同的方式显示当前工作目录

我有这么远:

[[ `pwd -P` = `pwd` ]] && echo "\[1;31m\]$(pwd -P)" || echo "\[1;32m\]$(pwd)"

将返回所需的输出,但它不能替代命令提示符中的 \w

我尝试用反引号将它包装起来,但这只会导致 PS1 中的 pwd -Ppwd

如果它是 符号链接(symbolic link) 或不是,我想有条件地更改颜色和值,这就是为什么我想要 if/else 类型决策。

最佳答案

如果您希望这实际上高效(并且提示应该快速呈现!),那么您需要缓存物理查找,并使用$PWD 而不是逻辑的 $(pwd)

# global variables are prefixed with funcname__ to avoid conflicts

# look up color codes for our terminal rather than assuming ANSI
set_prompt__red=$(tput setaf 1)
set_prompt__green=$(tput setaf 2)
set_prompt__reset=$(tput sgr0)

set_prompt() {

# only rerun this code when changing directories!
if [[ $set_prompt__lastPWDl != "$PWD" ]]; then
set_prompt__lastPWDl=$PWD
set_prompt__lastPWDp=$(pwd -P)
if [[ "$set_prompt__lastPWDl" = "$set_prompt__lastPWDp" ]]; then
set_prompt__pwd_color="$set_prompt__red"
else
set_prompt__pwd_color="$set_prompt__green"
fi
fi

# ...actually could have just "return"ed above, but this way we can change other
# aspects of the prompt even when we don't need to do a new directory lookup.
PS1='...whatever prefix...'
PS1+="\[${set_prompt__pwd_color}\]${PWD}\[${set_prompt__reset}\]"
PS1+='...whatever suffix...'
}
PROMPT_COMMAND=set_prompt

关于bash - PS1 定义中的条件密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49882286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com