【问题标题】:How to add an icon to the bash prompt如何在 bash 提示符中添加图标
【发布时间】:2012-11-19 18:32:37
【问题描述】:

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

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

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

【问题讨论】:

    标签: linux bash shell fedora ps1


    【解决方案1】:

    实际上,是的,你可以。

    在最近的 Bash 版本中,至少有 4 个(我可以在 4.2 和 4.3 中做到), 您可以使用十六进制渲染表情符号。

    使用echo -e 标志。

    粘贴emoji you looked up 并执行十六进制转储以查看其构成:

    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? ??
    

    我实际上将我的修改为:

    等离子〜⚡

    所以是的,想出一个这样的并尝试将其添加到您的 .bashrc.bash_profile

    编辑:SO 或浏览器渲染的某些内容可能已经改变,因为这篇文章中的标志现在呈现为“美国”字符。 YMMV,但我认为它仍然可以在指定的 bash 版本中工作。

    【讨论】:

    • hexdump 的某些版本可能需要使用 -C 选项运行以尊重字节序。 unix.stackexchange.com/questions/55770/…
    • 以防万一。如果我仍然可以访问我运行这些的环境,我会使用 hexdump 运行一些测试,看看我还能完成什么。我目前的环境是通过腻子,所以我的角色更加有限。如果有人知道如何扩展腻子角色支持,我会全力以赴。
    • @Plasmarob,我应该对我的 PS1 中的那个十六进制代码做一些特别的事情吗?我把它粘贴进去了,但终端只显示了那个确切的字符串。
    • @AndrewKirna 从一个地方复制/粘贴到另一个地方经常会导致问题。一个很好的初步测试是尝试将表情符号粘贴到命令行上,看看它是如何呈现(或不呈现)
    • @LonnieBest 看起来您的控制台不支持它。您可能使用的是较旧的控制台,或者您可能没有使用 Bash 4.x,最后我检查了许多系统从未离开 3.x - 最后我检查了 Apple 仍然使用他们自己的 bash 3.x 分支。你可以自己手动安装 bash 4.x,我以前是在 macbook 上安装的。
    【解决方案2】:

    现在,如果您有可识别表情符号的字体,则可以添加表情符号。我想当问题最初发布时,这不是一个容易可行的选择

    I wrote this blog post about it 几年前。

    我不知道美国国旗,但export PS1="\360\237\232\251 &gt; " 在您的提示中获得了一面旗帜。

    我还编写了一个 shell 工具,使打印 echo 或 shell prompt 的转义更容易一些。它叫emo

    【讨论】:

    • 那篇博文现在不见了。
    • 啊,原来如此,谢谢!博客迁移的受害者。现在在这里beatworm.co.uk/blog/computers/Cool-Shell-Prompts(我猜现在区分大小写),但截图不见了,我会在更正链接之前看看是否能找到它们
    【解决方案3】:

    距离越近,白旗和黑旗越接近:

    ⚐ → U+2690 ; WHITE FLAG
    ⚑ → U+2691 ; BLACK FLAG
    

    以上是 Unicode 字符。您还可以寻找带有标志的字体 - 我不知道。

    【讨论】:

    • 出于这个原因,也可以破解字体以支持不存在的字符。就像用图像编辑随机代码点一样。不过做了这么多无用的工作。
    【解决方案4】:

    无法在 bash 提示符中添加图标(位图或矢量图形)。

    【讨论】:

      【解决方案5】:

      对不起,没有。终端不做图形。

      有关您可以做什么的完整描述,请参阅 bash(1) 手册页的 PROMPTING 部分:

      提示

      当以交互方式执行时,bash 在准备好读取命令时显示主要提示 PS1,而在需要更多输入以完成命令时显示辅助提示 PS2。 Bash 允许通过插入一些反斜杠转义的特殊字符来自定义这些提示字符串,这些特殊字符被解码如下:

      \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
      

      命令号和历史号通常是不同的:命令的历史号是它在历史列表中的位置,其中可能包括从历史文件中恢复的命令(见下面的HISTORY),而命令号是位置在当前 shell 会话期间执行的命令序列中。字符串解码后,通过参数扩展、命令替换、算术扩展和引号删除等方式进行扩展,具体取决于 promptvars shell 选项的值(参见下文 SHELL BUILTIN COMMANDS 下 shopt 命令的说明)。

      \e\[\] 转义序列值得特别注意。有了这些,您可以插入ANSI escape codes 来命令终端更改前景色、背景色、移动光标、擦除部分屏幕,以及做其他花哨的技巧。

      也就是说,例如,您的提示如何改变颜色。 \[\e[0;31m\] 将前景色设置为红色,\[\e[0;0m\] 将其重置为默认值。

      【讨论】:

      • 这并不完全准确,因为一些现代终端确实支持表情符号,例如前面提到的美国国旗。试试: echo -e '\xf0\x9f\x87\xba\xf0\x9f\x87\xb8'
      【解决方案6】:

      如果您希望能够做到这一点,您可以搜索大量不同的图标。

      https://emojipedia.org/

      我从那个网站得到这个。 ??

      【讨论】:

        猜你喜欢
        • 2018-02-15
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 2022-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-21
        相关资源
        最近更新 更多