Bash 配置文件设置不适用于路径 - PS1

发布于 2024-12-15 12:38:06 字数 2249 浏览 0 评论 0原文

我编写了以下 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;

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-12-22 12:38:06

您需要导出 newPWD。仅仅因为您可以echo,并不意味着它在环境中。这意味着它在 shell 和/或环境中的设置。要知道我会使用的环境中是否有某些东西:

env | grep '^newPWD'

我不确定,但我不认为像这样使用 newPWD 会起作用,因为 newPWD 是不会像 PWD 那样每次更改目录时都会重置。因此,您最终可能需要更改 PS1 设置以每次调用 prompt_command 函数。

从您的评论来看,我对 newPWD 的猜测是正确的。因此,您需要做的是将 prompt_command 函数更改为 echo 您想要的提示。然后将您的 PS1 设置更改为

PS1="\`if [ \$? = 0 ];
                then
                    echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $(prompt_command) $GREEN $DEFAULT_COLOR';
                else
                    echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $(prompt_command) $RED $DEFAULT_COLOR';
              fi; \`"
export PS1;

注意:我还从 $RED$$GREEN$ 中删除了尾随的 $因为我认为它们是错别字。不确定这是否正确

You need to export newPWD. Just because you can echo 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:

env | grep '^newPWD'

I'm not sure but I don't think that using newPWD like this is going to work, as newPWD is not reset every time you change directory like PWD is. So you may end up needing to change your PS1 setting to call the prompt_command function each time.

From your comments looks like my guess about newPWD was right. So what you need to do is change the prompt_command function to echo the prompt that you want. Then change your PS1 setting to

PS1="\`if [ \$? = 0 ];
                then
                    echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $(prompt_command) $GREEN $DEFAULT_COLOR';
                else
                    echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $(prompt_command) $RED $DEFAULT_COLOR';
              fi; \`"
export PS1;

Note: I also removed the trailing $ from $RED$ and $GREEN$ as I assumed they were typos. Not sure if that was right

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文