如何在 bash 提示中添加图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13459913/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add an icon to the bash prompt
提问by imkendal
I know you can edit the bash prompt permanently by editing the PS1 variable in your ~/.bashrc file, mine looks like this:
我知道你可以通过编辑 ~/.bashrc 文件中的 PS1 变量来永久编辑 bash 提示,我的看起来像这样:
PS1="\[\e[0;31m\]<HERP(._.)DERP>\[\e[0;0m\]";
but can you set a tiny little image in there as well? For instance, if I wanted to add a little American flag icon, or something before "HERP(._.)DERP", could I do this?
但是你也可以在那里设置一个小小的图像吗?例如,如果我想添加一个小美国国旗图标,或者在“HERP(._.)DERP”之前添加一些东西,我可以这样做吗?
采纳答案by John Kugelman
Sorry, no. Terminals don't do graphics.
抱歉,没有。终端不做图形。
For a full description of what you cando, see the PROMPTINGsection of the bash(1) man page:
有关您可以执行的操作的完整说明,请参阅bash(1) 手册页的PROMPTING部分:
PROMPTING
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:
\a an ASCII bell character (07) \d the date in "Weekday Month Date" format (e.g., "Tue May 26") \D{format} the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required \e an ASCII escape character (033) \h the hostname up to the first ‘.' \H the hostname \j the number of jobs currently managed by the shell \l the basename of the shell's terminal device name \n newline \r carriage return \s the name of the shell, the basename of(the portion following the final slash) \t the current time in 24-hour HH:MM:SS format \T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format \A the current time in 24-hour HH:MM format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patch level (e.g., 2.00.0) \w the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable) \W the basename of the current working directory, with $HOME abbreviated with a tilde \! the history number of this command \# the command number of this command $ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn \ a backslash \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt \] end a sequence of non-printing characters\a an ASCII bell character (07) \d the date in "Weekday Month Date" format (e.g., "Tue May 26") \D{format} the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required \e an ASCII escape character (033) \h the hostname up to the first ‘.' \H the hostname \j the number of jobs currently managed by the shell \l the basename of the shell's terminal device name \n newline \r carriage return \s the name of the shell, the basename of(the portion following the final slash) \t the current time in 24-hour HH:MM:SS format \T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format \A the current time in 24-hour HH:MM format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patch level (e.g., 2.00.0) \w the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable) \W the basename of the current working directory, with $HOME abbreviated with a tilde \! the history number of this command \# the command number of this command $ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn \ a backslash \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt \] end a sequence of non-printing charactersplasmarob ~ $ echo -n ""| hexdump 0000000 f0 9f 87 ba f0 9f 87 b8 0000008The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see HISTORY below), while the command number is the position in the sequence of commands executed during the current shell session. After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell option (see the description of the shopt command under SHELL BUILTIN COMMANDS below).
提示
以交互方式执行时,bash 在准备好读取命令时显示主要提示 PS1,而在需要更多输入以完成命令时显示辅助提示 PS2。Bash 允许通过插入许多反斜杠转义的特殊字符来自定义这些提示字符串,这些特殊字符解码如下:
plasmarob ~ $ echo -e 'See? \xf0\x9f\x87\xba\xf0\x9f\x87\xb8' See?命令号和历史号通常是不同的:一个命令的历史号是它在历史列表中的位置,它可能包括从历史文件中恢复的命令(见下面的HISTORY),而命令号是它在序列中的位置在当前 shell 会话期间执行的命令。字符串解码后,根据 promptvars shell 选项的值,通过参数扩展、命令替换、算术扩展和引号删除进行扩展(请参阅下面的 SHELL BUILTIN COMMANDS 下的 shopt 命令说明)。
The \e, \[and \]escape sequences deserve special attention. With these you can insert ANSI escape codesto command the terminal to change foreground color, background color, move the cursor, erase parts of the screen, and do other fancy tricks.
的\e,\[和\]转义序列值得特别注意。使用这些,您可以插入ANSI 转义码来命令终端更改前景色、背景色、移动光标、擦除部分屏幕以及执行其他花哨的技巧。
That is, for instance, how your prompt changes color. \[\e[0;31m\]sets the foreground color to red, and \[\e[0;0m\]resets it back to the default.
也就是说,例如,您的提示如何更改颜色。\[\e[0;31m\]将前景色设置为红色,并将其\[\e[0;0m\]重置为默认值。
回答by Plasmarob
Actually, Yes, you can.
事实上,是的,你可以。
In recent versions of Bash, at least 4 (i could do it in 4.2 and 4.3), you can render emoji with the hex.
在最新版本的 Bash 中,至少有 4 个(我可以在 4.2 和 4.3 中做到),您可以使用十六进制呈现表情符号。
Use the echo -eflag.
使用echo -e旗帜。
paste an emoji you looked upin and do a hexdump to see what it's made of:
粘贴您查找的表情符号并执行 hexdump 以查看它的组成:
? → U+2690 ; WHITE FLAG
? → U+2691 ; BLACK FLAG
And then take that top line and escape each hex pair with \x :
然后取顶行并使用 \x 转义每个十六进制对:
##代码##I actually modified mine to be:
我实际上将我的修改为:
plasmarob ~ ?
等离子机器人~?
So yes, come up with one like this and try adding it to your .bashrcor .bash_profile.
所以是的,想出一个这样的,并尝试将它添加到您的.bashrc或.bash_profile.
回答by cms
Nowadays, you can add emoji if you have an emoji-aware font. I guess this wasn't a easily viable option when the question was originally posted
如今,如果您有可识别表情符号的字体,则可以添加表情符号。我猜当问题最初发布时,这不是一个容易可行的选择
I wrote this blog post about ita couple of years ago.
I don't know about American flags, but export PS1="\360\237\232\251 > "gets a flag in your prompt. 
我不知道美国国旗,但export PS1="\360\237\232\251 > "在你的提示中得到一面旗帜。
I also wrote a shell tool to make printing the escapes for echo or shell prompt a little easier. It's called emo
我还编写了一个 shell 工具,使打印 echo 或 shell prompt 的转义更容易一些。这叫emo
回答by ninjalj
The closer you'll get are white and black flags:
你越接近白旗和黑旗:
##代码##The above are Unicode characters. You could also look for a font with flags - I don't know of any.
以上是Unicode字符。您也可以寻找带有标志的字体 - 我不知道任何字体。
回答by Goddard
If you want to be able to do this you can search for a ton of different icons.
如果您希望能够做到这一点,您可以搜索大量不同的图标。
I got this from that site.
我从那个网站得到了这个。
回答by b3h3m0th
there is no way to add an icon (bitmap or vector graphic) to the bash prompt.
无法向 bash 提示符添加图标(位图或矢量图形)。

