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:
hostnamegets the name of the current computer.cksumgives me a checksum of its input, which I’m basically just using as a well-known hash function with numerical output.cutpicks 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.\hactually displays the hostname in the appropriate color, and\[\e[0m\]resets the colors to default.\wprints my working directory and\umy 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
PS1variable.
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.
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. :-)
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]
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 ?
Thanks. I applied this to my own bashrc: http://github.com/avar/dotfiles/commit/e40de4
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…
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.
sps :neutral:…
“http://en.patriot-cccp.ru/index.php?option=com_content&view=article&id=7866&lol=guard@kskzs1.act”>.…
tnx :?…
sail@ehwnc5.wise” rel=”nofollow”>.…
hello :cool:…
:|…
sps 8O…
:x…спс за инфу :idea:…
:evil:…
good :lol:…
:oops:…
спс :grin:…
:neutral:…
спс за инфу :P…
:oops:…
сэнкс за инфу :razz:…
…
спасибо за инфу :x…
…
спасибо за инфу :?:…
:o…
спс за инфу :P…