Bash Prompt HOWTO
Total Page:16
File Type:pdf, Size:1020Kb
Bash Prompt HOWTO $Revision$, $Date$ Giles Orr Bash Prompt HOWTO: $Revision$, $Date$ by Giles Orr Copyright © 1998, 1999, 2000, 2001, 2003 Giles Orr Creating and controlling terminal and xterm prompts is discussed, including incorporating standard escape sequences to give username, current working directory, time, etc. Further suggestions are made on how to modify xterm title bars, use external functions to provide prompt information, and how to use ANSI colours. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Table of Contents 1. Introduction and Administrivia...........................................................................................................1 1.1. Introduction.................................................................................................................................1 1.2. Revision History..........................................................................................................................1 1.3. Requirements..............................................................................................................................1 1.4. How To Use This Document.......................................................................................................2 1.5. Document Versions, Comments and Suggestions.......................................................................2 1.6. Problems......................................................................................................................................2 1.7. Credits/Bibliography...................................................................................................................3 1.8. Disclaimer...................................................................................................................................4 2. Bash and Bash Prompts........................................................................................................................5 2.1. What is Bash?..............................................................................................................................5 2.2. What Can Tweaking Your Bash Prompt Do For You?................................................................5 2.3. Why Bother?...............................................................................................................................5 2.4. The First Step..............................................................................................................................5 2.5. Bash Prompt Escape Sequences..................................................................................................6 2.6. Setting the PS? Strings Permanently...........................................................................................8 3. Bash Programming and Shell Scripts..................................................................................................9 3.1. Variables......................................................................................................................................9 3.2. Quotes and Special Characters....................................................................................................9 3.3. Command Substitution..............................................................................................................10 3.4. Non-Printing Characters in Prompts.........................................................................................10 3.5. Sourcing a File..........................................................................................................................11 3.6. Functions, Aliases, and the Environment..................................................................................12 4. External Commands............................................................................................................................14 4.1. PROMPT_COMMAND............................................................................................................14 4.2. External Commands in the Prompt...........................................................................................14 4.3. What to Put in Your Prompt......................................................................................................15 5. Saving Complex Prompts....................................................................................................................17 6. ANSI Escape Sequences: Colours and Cursor Movement..............................................................19 6.1. Colours......................................................................................................................................19 6.2. Cursor Movement......................................................................................................................21 6.3. Xterm Title Bar Manipulations.................................................................................................23 6.4. Xterm Title Bars and Screen.....................................................................................................24 6.5. Colours and Cursor Movement With tput ................................................................................25 7. Special Characters: Octal Escape Sequences....................................................................................29 8. The Bash Prompt Package..................................................................................................................31 8.1. Availability................................................................................................................................31 8.2. Xterm Fonts...............................................................................................................................31 8.3. Changing the Xterm Font..........................................................................................................31 8.4. Line Draw Characters without VGA Fonts...............................................................................32 iii 9. Loading a Different Prompt................................................................................................................33 9.1. Loading a Different Prompt, Later............................................................................................33 9.2. Loading a Different Prompt, Immediately................................................................................33 9.3. Loading Different Prompts in Different X Terms.....................................................................33 10. Loading Prompt Colours Dynamically............................................................................................35 10.1. A "Proof of Concept" Example...............................................................................................35 11. Prompt Code Snippets.......................................................................................................................37 11.1. Built-in Escape Sequences......................................................................................................37 11.2. Date and Time.........................................................................................................................37 11.3. Counting Files in the Current Directory.................................................................................37 11.4. Total Bytes in the Current Directory.......................................................................................38 11.5. Checking the Current TTY.....................................................................................................39 11.6. Stopped Jobs Count.................................................................................................................39 11.7. Load........................................................................................................................................40 11.8. Uptime.....................................................................................................................................41 11.9. Number of Processes...............................................................................................................42 11.10. Controlling the Size and Appearance of $PWD...................................................................42 11.11. Laptop Power........................................................................................................................43 11.12. Having the Prompt Ignored on Cut and Paste.......................................................................43 11.13. New Mail...............................................................................................................................44 11.14. Prompt Beeps After Long-Running Commands...................................................................44 12. Example Prompts...............................................................................................................................47 12.1. Examples on the Web..............................................................................................................47 12.2. A "Lightweight" Prompt.........................................................................................................47