在上一个命令失败后创建带有红色 $ 的 Bash 命令提示符
我是 Bash 编程新手,正在致力于创建自定义 Bash 命令提示符。我的目标是创建一个提示,仅在登录名和主机名与我通常使用的不同时显示它们。当位于 Git 版本控制下的目录中时,我还希望将当前的 Git 分支附加到命令提示符。
我想将登录名和主机名部分设置为绿色,目录路径设置为蓝色,Git 分支部分设置为粉色,分隔符(: 和 $ 字符)设置为白色。但是,当先前执行的命令返回零以外的任何内容时,我想将 $ 分隔符设置为红色。没有颜色的一般格式如下所示:
loginname@hostname:~/current/path:branchname$
唯一必需的部分是目录路径和 $ 字符。这是我为 .bashrc 文件编写的代码:
MYNAME="gwen"
MYHOST="gwen-laptop"
RED="\[\033[31m\]"
WHITE="\[\033[0m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
PINK="\[\033[01;35m\]"
DOLLAR="if [ \$? = 0 ]; then echo ${WHITE}\$; else echo ${RED}\$${NORMAL}; fi"
GITBRN='$(__git_ps1 "\033[0m:\033[01;35m%s")'
USERNM="if [ \u != ${MYNAME} ]; then whoami; fi;"
HOSTNM="if [ \h != ${MYHOST} ]; then echo -n @; hostname; fi;"
COLONM="if [ \u != ${MYNAME} ] || [ \h != ${MYHOST} ]; then echo -n :; fi;"
PS1="${GREEN}\`${USERNM}\`\`${HOSTNM}\`${WHITE}\`${COLONM}\`${BLUE}\w${GITBRN}\`${DOLLAR}\` "
此代码满足我的所有要求,除了它始终将 $ 字符保留为白色,并且不会在适当的时间。 (我怀疑问题是DOLLAR中的“\$?”引用了之前执行的命令,但是DOLLAR是在构建PS1时最后执行的,所以之前的执行语句不再是PS1构建开始之前运行的命令;这是一些东西这是为了创建命令提示符而执行的。)我不知道如何解决这个问题。
这段代码很丑陋,需要重构。我试图将所有颜色代码移到它们自己的变量中,但是当我在 GITBRN 代码中使用这些颜色变量时,事情变得混乱,所以我最终在那里使用了文字颜色。
我花了一整天的时间试图让这段代码正常工作,但我想此时我无处可去。任何关于如何在适当的时间将美元符号变成红色的建议将不胜感激。我也愿意接受有关重构代码以使其更清晰、更具可读性的建议。
PS 我是 Ubuntu Linux (Lucid Lynx) 用户。
I'm new to Bash programming, and I'm working on creating a custom Bash command prompt. My goal is to create a prompt which only shows the login name and host name when they differ from what I normally use. I'm also looking to append the current Git branch to the command prompt when in a directory which is under Git version control.
I would like to color the login and host name section green, the directory path blue, the Git branch section pink, and separators (: and $ characters) white. However, when the previously executed command returns anything other than zero, I would like to color the $ separator red. The general format without colors looks like this:
loginname@hostname:~/current/path:branchname$
The only sections that are mandatory are the directory path and the $ character. Here's the code I've written for my .bashrc file:
MYNAME="gwen"
MYHOST="gwen-laptop"
RED="\[\033[31m\]"
WHITE="\[\033[0m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
PINK="\[\033[01;35m\]"
DOLLAR="if [ \$? = 0 ]; then echo ${WHITE}\$; else echo ${RED}\${NORMAL}; fi"
GITBRN='$(__git_ps1 "\033[0m:\033[01;35m%s")'
USERNM="if [ \u != ${MYNAME} ]; then whoami; fi;"
HOSTNM="if [ \h != ${MYHOST} ]; then echo -n @; hostname; fi;"
COLONM="if [ \u != ${MYNAME} ] || [ \h != ${MYHOST} ]; then echo -n :; fi;"
PS1="${GREEN}\`${USERNM}\`\`${HOSTNM}\`${WHITE}\`${COLONM}\`${BLUE}\w${GITBRN}\`${DOLLAR}\` "
This code meets all of my requirements, except that it leaves the $ character white at all times, and does not color it red at the appropriate times. (I suspect the problem is that the "\$?" in DOLLAR references the previously executed command, but DOLLAR is executed last when constructing PS1, so the previously execute statement is no longer the command which was run before PS1 construction began; it's something which was executed in order to create the command prompt.) I'm not sure how to solve this problem.
This code is ugly and needs to be refactored. I was trying to move all the color codes into their own variables, but when I used these color variables in the code for GITBRN, thing went haywire, so I ended up using literal colors there instead.
I've spent an entire day trying to get this code working, and I think I'm going nowhere at this point. Any suggestions for how to get that dollar sign colored red at the appropriate time would be most appreciated. I'm also open to suggestion on refactoring the code to make it cleaner and more readable.
P.S. I'm a Ubuntu Linux (Lucid Lynx) user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
PROMPT_COMMAND
变量,根据 bash 手册页,该变量在每个主提示符之前执行。例如(这还不起作用,我试图让它正常工作,但我认为这是可能的):
编辑:由于在
PS1 内执行命令和非打印字符时遇到挫折
(\[
和\]
有时会按字面意思打印出来,而不是用作PS1
的提示),我来了与此一起(将...
替换为无论您在提示中想要什么):当然,使用
$()
您可以输入您喜欢在PS1
中使用其中的部分内容,而不是使用PROMPT_COMMAND
,我只是喜欢这种方式,以便PROMPT_COMMAND
包含逻辑和PS1
包含显示命令。Use the
PROMPT_COMMAND
variable, which is executed before each primary prompt according to the bash man page.For example (this doesn't work yet, I'm trying to get it working right, but I think it's possible):
Edit: due to frustrations with executing commands and nonprinting characters inside
PS1
(the\[
and\]
sometimes get printed out literally instead of used as hints toPS1
), I've come up with this (replace...
with whatever you want in your prompt):Of course, using
$()
you could put whichever parts of this you like insidePS1
instead of usingPROMPT_COMMAND
, I just like it this way so thatPROMPT_COMMAND
contains the logic andPS1
contains the display commands.我让它工作:
站在 @jtbandes 的支持上。 @jtbandes 是这个想法的原始作者。
I got it to work:
Standing on the sholders of @jtbandes. @jtbandes is the original author of the idea.
这是 Alexandr 的答案,修改为仅在命令失败时显示红色,而不是在空命令行上按 Enter 时显示红色,正如斯蒂芬所要求的。
Here is Alexsandr's answer modified to display the red color only when a command fails and not when you push enter on an empty command line, as Stéphane requested.