Changing Your bash Prompt


Changing Your bash Prompt

Depending on your Unix system, by default you might see as your prompt just a dollar sign ($), or perhaps a dollar sign and date, or other information as outlined in the Setting Your bash Prompt Promptly sidebar. You can set your prompt to include information that's handy for you.

Code Listing 8.13. Use grep to search your configuration files for a prompt statement.

[ejr@hobbes ejr]$ grep PS1 ~/.bash*  ~/.bashrc /etc/bashrc /home/ejr/.bashrc:PS1="\u \d $ " /etc/bashrc:PS1="[\u@\h \W]$ " [ejr@hobbes ejr]$ 

You actually have multiple prompts in bash:

  • The main prompt that you usually think of as the shell prompt. This prompt is called PS1.

  • A secondary prompt that you see when the system requires additional information to complete a command. Logically, this prompt is called PS2.

You can change either of these prompts using the following steps. You start by finding your prompt statement (Code Listing 8.13), then modifying it in your editor (Figure 8.4).

Figure 8.4. Edit your bash prompt statement in the editor of your choice.


To Change Your bash Prompt:

1.

grep PS1 ~/.bash* "~/.bashrc  /etc/bashrc 




To begin, search through the configuration files located in your home directory and in the /etc directory to find your prompt statement. It'll look something like PS1="$ " or PS1="[\u@\h \W]$ ", as shown in Code Listing 8.13.

The Setting Your bash Prompt Promptly sidebar will help translate these symbols.

2.

vi ~/.bashrc

Because the files with the prompt setting are in the systemwide /etc directory, we cannot change them directly, so we have to make the changes to .bashrc or a different configuration file in our home directory.

3.

PS1="\u \d $ "

For example, we often set our prompt to include the userid (because we have enough different accounts on different systems that we need a reminder) and the date (because we're scattered). We're adding this at the end of the file so it will take precedence over the PS1 setting in the /etc/bashrc file that is referenced from the ~/.bashrc file (Figure 8.4).

4.

Save the file and exit from the editor.

5.

su - ejr

Log in again with your changed prompt to try it out.

Tips

  • Note the trailing space in the prompt code: PS1="\u \d $ ". This space can help make it easier to use the prompt because it keeps your commands from bumping into your prompt.

  • Consider changing your PS1 environment variable at the shell prompt, as discussed in Chapter 3, before you make changes in your configuration files. This way, you can try out a modified shell prompt before you change it in your configuration files.


Setting Your bash Prompt Promptly

You can set your prompt to contain all sorts of information. The following list shows you what code to use to add certain kinds of information to your prompt (as well as help you translate the code in your existing prompt):

  • \u shows the userid of the current userthat's you.

  • \w shows the current working directory with a path, using a ~ notation within your home directory.

  • \W shows the current directory without the path.

  • \t shows the time.

  • \d shows the date.

  • \n forces a new line, making the prompt appear split on two lines.

  • \h shows the host name of the computer.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net