PS1 定义中的条件密码
Conditional pwd in an PS1 definition
我想在我的提示中显示 当前工作目录 ,如果我在 symlink
与不在
我已经做到了:
[[ `pwd -P` = `pwd` ]] && echo "\[1;31m\]$(pwd -P)" || echo "\[1;32m\]$(pwd)"
将 return 获得所需的输出,但它不能替代命令提示符中的 \w
。
我尝试用反引号将其包装起来,但这只会导致 PS1
.
中的 pwd -Ppwd
我想有条件地更改颜色和值,如果它是 symlink
或不是,这就是为什么我想要 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
这就是我最终得到的结果:
我想更改 pwd
值以及 symlink
时的颜色。
# look up color codes for our terminal rather than assuming ANSI
declare -r red=$(tput setaf 1)
declare -r green=$(tput setaf 2)
declare -r white=$(tput setaf 9)
declare -r aqua=$(tput setaf 6)
declare -r reset=$(tput sgr0)
colorize_msg() {
printf -v "\[%s\]%s"
}
set_prompt() {
declare prompt_pwd=""
# only rerun this code when changing directories!
if [[ last_prompt_pwdL != $PWD ]]; then
declare -g last_prompt_pwdL=$PWD # logical path
declare -r last_prompt_pwdP=$(pwd -P) # physical path
if [[ $last_prompt_pwdL = $last_prompt_pwdP ]]; then
colorize_msg prompt_pwd $green $last_prompt_pwdL
else
colorize_msg prompt_pwd $red $last_prompt_pwdP
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.
declare prompt=""
declare msg=""
colorize_msg msg $white "["
prompt+=$msg
colorize_msg msg $aqua "\u"
prompt+=$msg
colorize_msg msg $red "@"
prompt+=$msg
colorize_msg msg $aqua"\h"
prompt+=$msg
colorize_msg msg $white "] ["
prompt+=$msg
prompt+=${prompt_pwd}
colorize_msg msg $white "]"
prompt+=$msg
prompt+="${reset}\n"
PS1=$prompt
fi
}
我想在我的提示中显示 当前工作目录 ,如果我在 symlink
与不在
我已经做到了:
[[ `pwd -P` = `pwd` ]] && echo "\[1;31m\]$(pwd -P)" || echo "\[1;32m\]$(pwd)"
将 return 获得所需的输出,但它不能替代命令提示符中的 \w
。
我尝试用反引号将其包装起来,但这只会导致 PS1
.
pwd -Ppwd
我想有条件地更改颜色和值,如果它是 symlink
或不是,这就是为什么我想要 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
这就是我最终得到的结果:
我想更改 pwd
值以及 symlink
时的颜色。
# look up color codes for our terminal rather than assuming ANSI
declare -r red=$(tput setaf 1)
declare -r green=$(tput setaf 2)
declare -r white=$(tput setaf 9)
declare -r aqua=$(tput setaf 6)
declare -r reset=$(tput sgr0)
colorize_msg() {
printf -v "\[%s\]%s"
}
set_prompt() {
declare prompt_pwd=""
# only rerun this code when changing directories!
if [[ last_prompt_pwdL != $PWD ]]; then
declare -g last_prompt_pwdL=$PWD # logical path
declare -r last_prompt_pwdP=$(pwd -P) # physical path
if [[ $last_prompt_pwdL = $last_prompt_pwdP ]]; then
colorize_msg prompt_pwd $green $last_prompt_pwdL
else
colorize_msg prompt_pwd $red $last_prompt_pwdP
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.
declare prompt=""
declare msg=""
colorize_msg msg $white "["
prompt+=$msg
colorize_msg msg $aqua "\u"
prompt+=$msg
colorize_msg msg $red "@"
prompt+=$msg
colorize_msg msg $aqua"\h"
prompt+=$msg
colorize_msg msg $white "] ["
prompt+=$msg
prompt+=${prompt_pwd}
colorize_msg msg $white "]"
prompt+=$msg
prompt+="${reset}\n"
PS1=$prompt
fi
}