如何将图标添加到bash提示符

11

我知道您可以通过编辑~/.bashrc文件中的PS1变量来永久更改bash提示符,我的看起来像这样:

PS1="\[\e[0;31m\]<HERP(._.)DERP>\[\e[0;0m\]";

但是你能否在这里也设置一个小图像呢?例如,如果我想在“HERP(._.)DERP”之前添加一个美国国旗图标或其他图标,我可以这样做吗?

6个回答

16

实际上,是的,你可以。

在Bash的最新版本中,至少在4.2和4.3中,你可以使用十六进制渲染表情符号。

使用echo -e标志。

粘贴一个你查找的表情符号并进行十六进制转储以查看其构成:

plasmarob ~ $ echo -n ""| hexdump

0000000 f0 9f 87 ba f0 9f 87 b8                        
0000008

然后将顶部的那一行用 \x 转义每个十六进制对:

plasmarob ~ $ echo -e 'See? \xf0\x9f\x87\xba\xf0\x9f\x87\xb8'
See? 

我实际上修改了我的为:

plasmarob ~ ⚡

所以,是的,你可以像这样创建一个并尝试将其添加到你的.bashrc.bash_profile中。

编辑:由于此帖子中的标志现在呈现为“US”字符,因此可能有一些与SO或浏览器渲染相关的问题。 YMMV,但我认为它仍将在所述版本的bash中工作。


3
部分版本的 hexdump 可能需要使用 -C 选项来考虑大小端。参考链接:https://unix.stackexchange.com/questions/55770/does-hexdump-respect-the-endianness-of-its-system - Nik
@Plasmarob,我需要在我的PS1中对那个十六进制代码进行特殊处理吗?我将其粘贴进去,但终端只是显示了该字符串。 - Andrew Kirna
@AndrewKirna 复制/粘贴经常会导致问题。一个好的初步测试是尝试将表情符号粘贴到命令行中,看看它是如何呈现(或未呈现)的。 - Plasmarob
@Plasmarob,它可以正常粘贴,但在视觉上仍然占据两个字符的空间,而在bash中只注册一个空格。更多细节请参见我的帖子:https://dev59.com/ErHma4cB1Zd3GeqPMYHO - Andrew Kirna
1
@LonnieBest 看起来你的控制台不支持它。你可能正在使用较旧的控制台,或者很可能你没有使用 Bash 4.x,据我所知,许多系统从未离开 3.x - 据我所知,Apple 仍在使用他们自己的 bash 3.x 分支。你可以手动安装 bash 4.x,我曾经在 MacBook 上这样做过。 - Plasmarob
显示剩余2条评论

8
现在,如果你有一个支持表情符号的字体,你就可以添加表情符号了。我猜当最初发布这个问题时,这不是一个容易可行的选择。
我几年前写过一篇关于此的博客文章。链接如下:http://beatworm.co.uk/blog/computers/cool-shell-prompts/ 我不知道美国国旗怎么样,但是使用export PS1="\360\237\232\251 > "可以在你的提示符中显示一个国旗。
我还编写了一个shell工具来使echo或shell提示符的转义输出更加容易。它叫做emo

那篇博客文章现在已经消失了。 - Lonnie Best
1
啊,原来是这样,谢谢!博客迁移的受害者。现在它在这里 https://www.beatworm.co.uk/blog/computers/Cool-Shell-Prompts(现在区分大小写了),但是截图丢失了,我会看看能否在修正链接之前找到它们。 - cms

3
你所能看到的最接近的标志是白旗和黑旗:
⚐ → U+2690 ; WHITE FLAG
⚑ → U+2691 ; BLACK FLAG

上面是Unicode字符。您也可以寻找带有国旗的字体 - 我不知道有哪些。

为此,人们也可以黑客式地修改字体以支持不存在的字符。例如,将一个随机的代码点与图像进行编辑。但是这样做实际上是在做很多无用功。 - Camilo Martin

2
抱歉,终端不支持图形显示。
要了解您可以做什么,请参阅bash(1)手册中的提示部分的完整描述:

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 $0 (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

The 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).

\e\[\]这三个转义序列非常重要。使用它们,您可以插入ANSI转义代码来控制终端更改前景色、背景色、移动光标、擦除屏幕的部分内容以及进行其他花哨的技巧。
例如,您的提示符如何更改颜色就是通过这种方式实现的。\[\e[0;31m\]将前景色设置为红色,\[\e[0;0m\]将其重置回默认值。

5
有些现代终端支持表情符号,比如之前提到的美国国旗,但这并不是完全准确的。尝试输入以下命令:echo -e '\xf0\x9f\x87\xba\xf0\x9f\x87\xb8' - Plasmarob

1

如果你想要做到这一点,你可以搜索大量不同的图标。

https://emojipedia.org/

我从那个网站上得到了这个。

-2

无法向 Bash 提示符添加图标(位图或矢量图形)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接