bash登录远程服务器_登录服务器时如何使Bash提示更改颜色?

bash登录远程服务器

bash登录远程服务器

how-do-you-make-the-bash-prompt-change-colors-when-logged-into-a-server-00-final

If you access multiple servers throughout the work day, then being able to tell which one you are working with at a glance based on a color scheme can be very useful. Today’s SuperUser Q&A post helps a reader achieve a colorful nirvana in his work environment.

如果您在一整天的工作中访问多台服务器,那么根据颜色方案一目了然就可以知道正在使用哪台服务器,这将非常有用。 今天的超级用户问答环节可帮助读者在工作环境中实现丰富多彩的必杀技。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 (The Question)

SuperUser reader nitins wants to know how to make the Bash Prompt change colors when logged into a server:

超级用户阅读器nitins想知道如何在登录服务器时使Bash Prompt更改颜色:

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 Prompt动态化,以便在我登录服务器时可以更改颜色? 使用自己的个人系统时,我希望颜色为绿色,但是当我连接到服务器时,颜色会变为红色。 我有大量的服务器可以访问,并且不想在每个服务器上都放置不同的.bashrc

How do you make the Bash Prompt change colors when logged into a server?

登录服务器时如何使Bash Prompt更改颜色?

答案 (The Answer)

SuperUser contributor Matei David has the answer for us:

超级用户贡献者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:

远程Bash提示由远程〜/ .bashrc设置 ,因此您仍然需要将其复制到远程服务器。 但是,您可以对所有主机使用单个〜/ .bashrc并根据主机名设置Bash Prompt颜色:

how-do-you-make-the-bash-prompt-change-colors-when-logged-into-a-server-01

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

    如果尚未设置PS1,则不要设置它(即,如果外壳不是交互式的)。 测试PS1是否为“非空”是一种很常见的方法,可确定外壳是否是交互式的,并且您不想混淆执行该操作的程序(可以说,更准确的测试是检查$-是否包含 )。

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

    如果要让此代码在登录到远程服务器时运行,则应该让配置文件之一始终作为源〜/ .bashrc (我假设您知道)。

  • In PS1, the escape codes must be enclosed in \[…\].

    PS1中 ,转义代码必须包含在\ […\]中

  • \[033[m resets the foreground and background to their defaults, so here :\w appears in the terminal foreground/background.

    \ [033 [m将前景和背景重置为默认值,因此:\ w显示在终端的前景/背景中。

  • \[033[48;5;XXXm\033[38;5;YYYm sets the background/foreground to XXX/YYY.

    \ [033 [48; 5; XXXm \ 033 [38; 5; YYYm]将背景/前景设置为XXX / YYY

  • For a script that dumps the available colors, try colortest.

    对于转储可用颜色的脚本,请尝试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>$ “

    要检查并查看Bash Prompt的外观,请使用: echo -e“ <\ 033 [48; 5; 16m \ 033 [38; 5; 196mhost \ 033 [m:dir> $”



Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

Image Credit: Emx (Wikipedia)

图片来源: Emx(Wikipedia)

翻译自: https://www.howtogeek.com/270338/how-do-you-make-the-bash-prompt-change-colors-when-logged-into-a-server/

bash登录远程服务器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值