-->
Previous Table of Contents Next


Prompts

bash has two levels of user prompts. The first level is what you see when bash is waiting for a command to be typed. (This is what you normally see when you are working with bash.)

The default first-level prompt is the % character. If you do not like the % character as the prompt or prefer to customize your prompt, you can do so by setting the value of the PS1 bash variable. For example:


PS1=“Please enter a command”

sets the bash shell prompt to the specified string.

The second level of prompt is displayed when bash is expecting more input from you in order to complete a command. The default for the second-level prompt is >. If you want to change the second-level prompt, you can set the value of the PS2 variable, as in:


PS2=“I need more information”

In addition to displaying static character strings in the command prompts (as in the two preceding examples), you can also use some predefined special characters. These special characters place things such as the current time into the prompt. Table 11.1 lists the most commonly used special character codes.

Table 11.1. Prompt special character codes.

Character Meaning

\! Displays the history number of this command.
\# Displays the command number of the current command.
\$ Displays a $ in the prompt unless the user is root. When the user is root, it displays a #.
\\ Displays a backslash.
\d Displays the current date.
\h Displays the host name of the computer on which the shell is running.
\n Prints a newline character. This causes the prompt to span more than one line.
\nnn Displays the character that corresponds to the octal value of the number nnn.
\s The name of the shell that is running.
\t Displays the current time.
\u Displays the username of the current user.
\W Displays the base name of the current working directory.
\w Displays the current working directory.

These special characters can be combined into several useful prompts to provide you with information about where you are. (They can be combined in very grotesque ways, too!) Several examples of setting the PS1 prompt follow here:


PS1=“\t”

This causes the prompt to have the following appearance (there is no space after the prompt):


02:16:15

The prompt string


PS1=\t

causes the prompt to have the following appearance:


t

This shows the importance of including the character sequence in quotation marks. The prompt string


PS1=“\t\\ ”

causes the prompt to look like this:


02:16:30\

In this case, there is a space following the prompt because there was a space within the quotation marks.

Job Control

Job control refers to the ability to control the execution behavior of a currently running process. Specifically, you can suspend a running process and cause it to resume running later. bash keeps track of all processes that it starts (as a result of user input) and lets you suspend a running process or restart a suspended one at any time during the life of that process. (For more information on processes, see Chapter 34, “Processes.”)

Pressing Ctrl+Z suspends a running process. The bg command restarts a suspended process in the background, whereas the fg command restarts a process in the foreground.

These commands are most often used when a user wants to run a command in the background but accidentally starts it in the foreground. When a command is started in the foreground, it locks the shell from any further user interaction until the command completes execution. This is usually not a problem because most commands take only a few seconds to execute. If the command you are running takes a long time, though, start the command in the background so that you can continue to use bash to enter other commands in the foreground.

For example, if you start the command find / -name “test” > find.out (which scans the entire file system for files named test and stores the results in a file called find.out) in the foreground, your shell may be tied up for many seconds or even minutes, depending on the size of the file system and the number of users on the system. If you issue this command and want to continue executing in the background so you can use the system again, enter the following:


control-z

bg

This first suspends the find command, then restarts it in the background. The find command continues to execute, and you have bash back again.


Previous Table of Contents Next