Chapter 10. Prompt Code Snippets

This section shows how to put various pieces of information into the Bash prompt. There are an infinite number of things that could be put in your prompt. Feel free to send me examples, I'll try to include what I think will be most widely used. If you have an alternate way to retrieve a piece of information here, and feel your method is more efficient, please contact me. It's easy to write bad code, I do it often, but it's great to write elegant code, and a pleasure to read it. I manage it every once in a while, and would love to have more of it to put in here.

To incorporate shell code in prompts, it has to be escaped. Usually, this will mean putting it inside \$(<command>) so that the output of command is substituted each time the prompt is generated.

Please keep in mind that I develop and test this code on a fairly up-to-date single user desktop, so the delay generated by these code snippets doesn't usually mean much to me. To help with this I ran (for a while) a 25 MHz 486 SX with 16 meg of RAM, and you'll see the output of the time command for some of the code blocks to indicate how much of a delay it causes on a slower machine. I wish I still had that machine: most of these run fast enough that it doesn't matter, but it's hard to tell which ones might be an issue on heavily loaded or old machines because on modern machines time returns numbers so small they're statistically useless.

10.1. Built-in Escape Sequences

See Section 2.5 for a complete list of built-in escape sequences. This list is taken directly from the Bash man page, so you can also look there.