Bash 配置文件设置不适用于路径 - PS1
我编写了以下 bash 配置文件设置文件:
BLACK="\[\033[0;30m\]"
DARK_GRAY="\[\033[1;30m\]"
LIGHT_GRAY="\[\033[0;37m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_CYAN="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
LIGHT_RED="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
DEFAULT_COLOR="\[\033[00m\]"
function prompt_command {
# How many characters of the $PWD should be kept
local pwd_length=23
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then
newPWD="$(echo -n $PWD | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
else
newPWD="$(echo -n $PWD)"
fi
}
PROMPT_COMMAND=prompt_command;
PS1="\`if [ \$? = 0 ];
then
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $newPWD $GREEN$ $DEFAULT_COLOR';
else
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $newPWD $RED$ $DEFAULT_COLOR';
fi; \`"
export PS1;
由于某种原因,获取 newPWD 不起作用。它存在于环境中(我可以用 echo 打印它),但是它没有添加到命令行提示符中。总是添加一个空字符串。所以示例输出是:
[11:54:09] ber@szak: $
有什么想法为什么不呢?
附言。这是我的解决方案:
BLACK="\[\033[0;30m\]"
DARK_GRAY="\[\033[1;30m\]"
LIGHT_GRAY="\[\033[0;37m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_CYAN="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
LIGHT_RED="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
DEFAULT_COLOR="\[\033[00m\]"
export MYPATHPS='$(
echo -n "${PWD/#$HOME/~}" |
awk -F "/" '"'"'{
if (length() > 14) {
if (NF>4) print "/" "/.../" $(NF-1) "/" $NF;
else if (NF>3) print "/" "/.../" $NF;
else print "/.../" $NF;
}
else print ;
}'"'"'
)';
PS1="$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h \` if [ \$? = 0 ]
then
echo -en '$GREEN'
else
echo -en '$RED'
fi;
\` $MYPATHPS $ $DEFAULT_COLOR ";
export PS1;
I've written the following bash profile settings file:
BLACK="\[\033[0;30m\]"
DARK_GRAY="\[\033[1;30m\]"
LIGHT_GRAY="\[\033[0;37m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_CYAN="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
LIGHT_RED="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
DEFAULT_COLOR="\[\033[00m\]"
function prompt_command {
# How many characters of the $PWD should be kept
local pwd_length=23
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then
newPWD="$(echo -n $PWD | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
else
newPWD="$(echo -n $PWD)"
fi
}
PROMPT_COMMAND=prompt_command;
PS1="\`if [ \$? = 0 ];
then
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $newPWD $GREEN$ $DEFAULT_COLOR';
else
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $newPWD $RED$ $DEFAULT_COLOR';
fi; \`"
export PS1;
For some reason getting the newPWD is not working. It is present in the enviroment (I can print it with the echo), however it isn't added to the command lines prompt. There an empty string is added always. So a sample output is:
[11:54:09] ber@szak: $
Any ideas why not?
PS. Here it is my solution to this:
BLACK="\[\033[0;30m\]"
DARK_GRAY="\[\033[1;30m\]"
LIGHT_GRAY="\[\033[0;37m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_CYAN="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
LIGHT_RED="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
DEFAULT_COLOR="\[\033[00m\]"
export MYPATHPS='$(
echo -n "${PWD/#$HOME/~}" |
awk -F "/" '"'"'{
if (length() > 14) {
if (NF>4) print "/" "/.../" $(NF-1) "/" $NF;
else if (NF>3) print "/" "/.../" $NF;
else print "/.../" $NF;
}
else print ;
}'"'"'
)';
PS1="$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h \` if [ \$? = 0 ]
then
echo -en '$GREEN'
else
echo -en '$RED'
fi;
\` $MYPATHPS $ $DEFAULT_COLOR ";
export PS1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
导出
newPWD。仅仅因为您可以echo
,并不意味着它在环境中。这意味着它在 shell 和/或环境中的设置。要知道我会使用的环境中是否有某些东西:我不确定,但我不认为像这样使用
newPWD
会起作用,因为newPWD
是不会像PWD
那样每次更改目录时都会重置。因此,您最终可能需要更改PS1
设置以每次调用prompt_command
函数。从您的评论来看,我对 newPWD 的猜测是正确的。因此,您需要做的是将
prompt_command
函数更改为echo
您想要的提示。然后将您的PS1
设置更改为注意:我还从
$RED$
和$GREEN$
中删除了尾随的$
因为我认为它们是错别字。不确定这是否正确You need to
export
newPWD. Just because you canecho
it doesn't mean its in the environment. It means its set in the shell and/or the environment. To know if something is in the environment I would use:I'm not sure but I don't think that using
newPWD
like this is going to work, asnewPWD
is not reset every time you change directory likePWD
is. So you may end up needing to change yourPS1
setting to call theprompt_command
function each time.From your comments looks like my guess about
newPWD
was right. So what you need to do is change theprompt_command
function toecho
the prompt that you want. Then change yourPS1
setting toNote: I also removed the trailing
$
from$RED$
and$GREEN$
as I assumed they were typos. Not sure if that was right