Per-computer colored prompts in bash

I use my Athena account from many computers, often multiple computers at once via SSH, and it’s useful to keep track of which terminal is showing a prompt from which computer. One reasonably easy way to accomplish that is to color the prompt as a function of which computer I’m on. Here’s what I use in my .bashrc:


export PS1="\[\e[1;$((31 + $(hostname | cksum | cut -c1-3) % 6))m\]\h\[\e[0m\]:\w \u\$ "

From inside out:

  • hostname gets the name of the current computer.
  • cksum gives me a checksum of its input, which I’m basically just using as a well-known hash function with numerical output.
  • cut picks out certain characters from the input, in this case the first three digits/characters, because checksums can be fairly large.
  • $(...) means to evaluate a command and substitute it’s output. (If you’re familiar with backticks, it’s the same thing, except that $(... $(...)) works the way you want and `... `...`` doesn’t.)
  • $((...)) evaluates arithmetic. In this case, we take the checksum and get the remainder when divided by six and add it to 31, so we get a number between 31 and 36. This happens to be the range of ANSI color codes excluding white and black.
  • \e[1;35m (or whatever the number happens to be is the full ANSI escape sequence to turn the prompt a certain color, bolded.
  • \[ and \] tell bash that the intervening text in a prompt is a formatting code, and shouldn’t be counted towards the length of the prompt, although it doesn’t seem to work for me. It might be related to this snide observation about the code that displays the prompt; I’ve never looked into it in detail.
  • \h actually displays the hostname in the appropriate color, and \[\e[0m\] resets the colors to default.
  • \w prints my working directory and \u my username: I sometimes use this prompt or my .bashrc on other accounts, so it’s preferable not to hard-code it.
  • \$ prints a dollar sign most of the time, except that it prints a pound sign if the shell belongs to root: this is a classic UNIX way of distinguishing the superuser account. Again, I sometimes reuse my prompt, so it’s nice to have this flexibility.
  • Finally, I stuff the result into the PS1 variable.
  • So, I get something like this:

    kid-icarus:~ geofft$ logout
    Connection to kid closed.
    dr-wily:~ geofft$

    If you want more background on bash prompts in general, check out the section of the bash manual on prompts.

    Tags: , ,

18 Responses to “Per-computer colored prompts in bash”

  1. Edward Yang says:

    I think the conversion of checksum into color was very clever, though one must wonder if some of your servers look very similar color-wise by mere fluke. :-)

  2. Anonymous says:

    its the remainder that would make some of the colors the same; because theres a limited number of colors. if you had as many colors as MAX-CHECKSUM, then you’d likely be fine. My boxen collide [189 and 273 for the chksum | cut]

  3. Nadav says:

    Nice. I will make sure to try this. Do you have any tips on using screen (the unix util) when logging different servers ? Can you think of a clever way to change the title of each of the tabs ?

  4. geofft says:

    Nadav: What I currently use is basically exactly what the “TITLES (naming windows)” section of screen’s manpage suggests. However, the idea of setting the screen tab name to the server name (as opposed to just “ssh”) is kind of intriguing…

  5. geofft says:

    Edward/Anon: Yeah, I tweaked the number of characters I cut when I realized the first version (cut -c1-2, I believe) gave me annoying collisions on servers I use too often. :-) If you have 256-color terminals everywhere, you can probably be more clever than this. Or you could just special case some hostnames.

  6. harry says:

    < a href = :?

    sps :neutral:…

  7. carl says:

    “http://en.patriot-cccp.ru/index.php?option=com_content&view=article&id=7866&lol=guard@kskzs1.act”>.…

    tnx :?…

  8. dennis says:

    sail@ehwnc5.wise” rel=”nofollow”>.

    hello :cool:…

  9. ray says:

    :|

    sps 8O…

  10. alberto says:

    :x

    спс за инфу :idea:…

  11. jackie says:

    :evil:

    good :lol:…

  12. terrance says:

    :oops:

    спс :grin:…

  13. willard says:

    :neutral:

    спс за инфу :P…

  14. tony says:

    :oops:

    сэнкс за инфу :razz:…

  15. angelo says:


    :eek:

    спасибо за инфу :x…

  16. steve says:

    :lol:

    спасибо за инфу :?:…

  17. lawrence says:

    :o

    спс за инфу :P…

Leave a Reply