s i s t e m a o p e r a c i o n a l m a g n u x l i n u x | ~/ · documentação · suporte · sobre |
Next
Previous
Contents
2. Bash and Bash Prompts2.1 What is Bash?Descended from the Bourne Shell, Bash is a GNU product, the "Bourne Again SHell." It's the standard command line interface on most Linux machines. It excels at interactivity, supporting command line editing, completion, and recall. It also supports configurable prompts - most people realize this, but don't know how much can be done.
2.2 What Can Tweaking Your Bash Prompt Do For You?Most Linux systems have a default prompt in one colour (usually gray) that tells you your user name, the name of the machine you're working on, and some indication of your current working directory. This is all useful information, but you can do much more with the prompt: all sorts of information can be displayed (tty number, time, date, load, number of users, uptime ...) and the prompt can use ANSI colours, either to make it look interesting, or to make certain information stand out. You can also manipulate the title bar of an Xterm to reflect some of this information.
2.3 Why Bother?Beyond looking cool, it's often useful to keep track of system information. One idea that I know appeals to some people is that it makes it possible to put prompts on different machines in different colours. If you have several Xterms open on several different machines, or if you tend to forget what machine you're working on and delete the wrong files (or shut down the server instead of the workstation), you'll find this a great way to remember what machine you're on.
For myself, I like the utility of having information about my machine and work environment available all the time. And I like the challenge of trying to figure out how to put the maximum amount of information into the smallest possible space while maintaining readability.
2.4 The First StepThe appearance of the prompt is governed by the shell variable PS1. Command continuations are indicated by the PS2 string, which can be modified in exactly the same ways discussed here - since controlling it is exactly the same, and it isn't as "interesting," I'll mostly be modifying the PS1 string. (There are also PS3 and PS4 strings. These are never seen by the average user - see the Bash man page if you're interested in their purpose.) To change the way the prompt looks, you change the PS1 variable. For experimentation purposes, you can enter the PS1 strings directly at the prompt, and see the results immediately (this only affects your current session, and the changes go away when you log out). If you want to make a change to the prompt permanent, look at the section below Setting the PS? Strings Permanently.
Before we get started, it's important to remember that the PS1 string is stored in the environment like any other environment variable. If you modify it at the command line, your prompt will change. Before you make any changes, you can save your current prompt to another environment variable:
The simplest prompt would be a single character, such as:
This demonstrates the best way to experiment with basic prompts, entering them at the command line. Notice that the text entered by the user appears immediately after the prompt: I prefer to use
which forces a space after the prompt, making it more readable. To restore your original prompt, just call up the variable you stored:
2.5 Bash Prompt Escape SequencesThere are a lot of escape sequences offered by the Bash shell for insertion in the prompt. From the Bash 2.02 man page:
Continuing where we left off:
This is similar to the default on most Linux distributions. I wanted a slightly different appearance, so I changed this to:
2.6 Setting the PS? Strings PermanentlyVarious people and distributions set their PS? strings in different places. The most common places are /etc/profile, /etc/bashrc, ~/.bash_profile, and ~/.bashrc . Johan Kullstam ( johan19@idt.net) writes:
I tried to duplicate the problem he explains, and encountered a different one: my PROMPT_COMMAND variable (which will be introduced later) was blown away. My knowledge in this area is somewhat shaky, so I'm going to go with what Johan says.
Next Previous Contents |