如何使bash提示符在登录到服务器时更改颜色?

如果您在整个工作日访问多个服务器,那么能够根据配色方案一眼就知道您使用的是哪一个服务器是非常有用的。今天的超级用户问答帖子帮助读者在工作环境中实现多彩的涅盘。...

如果您在整个工作日访问多个服务器,那么能够根据配色方案一眼就知道您使用的是哪一个服务器是非常有用的。今天的超级用户问答帖子帮助读者在工作环境中实现多彩的涅盘。

今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。

问题

超级用户读者nitins想知道如何使Bash提示符在登录到服务器时更改颜色:

Is there a way to make the Bash Prompt dynamic so that it changes color when I am logged into a server? I want the color to be green when using my own personal system, but change to red whenever I am connected to servers. I have a huge number of servers that I access and do not want to have to put a different .bashrc on each and every one of them.

如何使Bash提示符在登录到服务器时更改颜色?

答案

超级用户贡献者Matei David为我们提供了答案:

Remote Bash Prompts are set by the remote ~/.bashrc, so you will still need to copy it to the remote servers. However, you can use a single ~/.bashrc for all hosts and set the Bash Prompt color based on the host name:

Notes

  • Do not set PS1 if it is not already set (i.e. if the shell is not interactive). Testing to see if PS1 is “non-empty” is a very common way to find out if the shell is interactive and you do not want to confuse programs that do that (arguably, a more accurate test is checking to see if $- contains i).
  • If you want this code to run when logging into a remote server, you should have one of the profile files always source ~/.bashrc (I am assuming you know that).
  • In PS1, the escape codes must be enclosed in \[…\].
  • \[033[m resets the foreground and background to their defaults, so here :\w appears in the terminal foreground/background.
  • \[033[48;5;XXXm\033[38;5;YYYm sets the background/foreground to XXX/YYY.
  • For a script that dumps the available colors, try colortest.
  • To check and see what the Bash Prompt would look like, use: echo -e “<\033[48;5;16m\033[38;5;196mhost\033[m:dir>$ “

有什么要补充的解释吗?在评论中发出声音。想从其他精通技术的Stack Exchange用户那里了解更多答案吗?在这里查看完整的讨论主题。

图片来源:Emx(维基百科)

  • 发表于 2021-04-09 06:06
  • 阅读 ( 214 )
  • 分类:互联网

你可能感兴趣的文章

Windows10中7种修复缓慢启动时间的方法

...休眠,因此它不会出现。要启用休眠,请打开管理员命令提示符或PowerShell窗口。您可以通过右键单击开始按钮或单击Win+X并选择命令提示符(Admin)或Windows PowerShell(Admin)来完成此操作。 ...

  • 发布于 2021-03-11 02:02
  • 阅读 ( 260 )

5次调整,使窗口更加丰富多彩

... 在过去,我们已经向您展示了如何更改命令提示符的颜色,您可以在这里看到更多独特的小贴士。你可以去掉所有的默认颜色,完全创建你自己的主题。 ...

  • 发布于 2021-03-14 12:05
  • 阅读 ( 225 )

Windows10中LinuxBashShell快速指南

...保持你的主文件夹完好无损。要完成此操作,请通过命令提示符或PowerShell运行以下命令: ...

  • 发布于 2021-03-17 01:06
  • 阅读 ( 189 )

如何定制mac终端并使其更有用

... 第二行是实际的命令提示符;末尾的矩形框是光标。您将在此行键入命令: ...

  • 发布于 2021-03-21 20:13
  • 阅读 ( 269 )

如何在macos终端中定制zsh提示符

... 您可以对zsh提示符进行修饰,使其最小化并充分利用空间。我们将向您展示如何定制终端应用程序中的zsh提示符,使其在您选择的任何背景中都脱颖而出。 ...

  • 发布于 2021-03-26 16:03
  • 阅读 ( 560 )

如何更改linux密码

...但幸运的是,更改Linux密码很容易。今天我们将向您展示如何通过几个简单的命令更改当前用户的密码、其他用户的密码和超级用户的密码。 ...

  • 发布于 2021-03-28 02:28
  • 阅读 ( 185 )

如何使用受限shell来限制linux用户可以做什么

...改其目录,您可以控制他们可以访问哪些命令。下面介绍如何在Linux上设置受限shell。 受限炮弹 受限shell不是另一个shell。这是标准外壳的另一种模式。Bash、Korn、Fish和其他shell都可以在受限shell模式下启动。在本文中,我们将...

  • 发布于 2021-03-31 10:57
  • 阅读 ( 383 )

如何使用chsh在linux上更改默认shell

...们启动FishShell时,会收到一条友好的欢迎消息,并且命令提示符会发生变化。可能令人惊讶的是,$SHELL环境仍然持有bash可执行文件/bin/bash的路径。没关系,这很正常。 启动新shell(或任何其他程序)时,它将继承父shell的环境。...

  • 发布于 2021-04-02 06:04
  • 阅读 ( 334 )

如何在linux上的bash中设置环境变量

...保存的设置定义了在终端窗口中找到的环境,一直到命令提示符的外观。所以,自然地,它们被称为环境变量。 一些环境变量是系统范围的,或者是全局的。其他人是全会,只能由你看到。其他人无法引用您的会话环境变量。...

  • 发布于 2021-04-02 06:44
  • 阅读 ( 218 )

如何在macos catalina上将默认shell更改为bash

有了macOS Catalina,苹果现在使用Zsh作为默认shell。我们喜欢Zsh,但是macOS中仍然包含可靠的老Bash shell,如果您愿意,可以快速切换回Bash。 Zsh只是新创建的用户帐户上的默认shell,因此在升级的Mac上的任何现有帐户在默认情况下仍...

  • 发布于 2021-04-03 01:32
  • 阅读 ( 197 )
宸宸最酷
宸宸最酷

0 篇文章

相关推荐