Free Hosting Online for WorkStations

< Previous | Contents | Next >

Anatomy Of A Prompt

Our default prompt looks something like this:



[me@linuxbox ~]$

[me@linuxbox ~]$


Notice that it contains our username, our hostname and our current working directory, but how did it get that way? Very simply, it turns out. The prompt is defined by an environ- ment variable named PS1 (short for “prompt string one”). We can view the contents of PS1 with the echo command:


[me@linuxbox ~]$ echo $PS1

[\u@\h \W]\$

[me@linuxbox ~]$ echo $PS1

[\u@\h \W]\$


Note: Don't worry if your results are not exactly the same as the example above. Every Linux distribution defines the prompt string a little differently, some quite exotically.


From the results, we can see that PS1 contains a few of the characters we see in our prompt such as the brackets, the at-sign, and the dollar sign, but the rest are a mystery. The astute among us will recognize these as backslash-escaped special characters like

Anatomy Of A Prompt


those we saw in Chapter 7. Here is a partial list of the characters that the bash treats spe- cially in the prompt string:


Table 13-1: Escape Codes Used In Shell Prompts


Sequence Value Displayed

Sequence Value Displayed

\a ASCII bell. This makes the computer beep when it is encountered.


\d Current date in day, month, date format. For example, “Mon May 26.”


\h Hostname of the local machine minus the trailing domain name.


\H Full hostname.


\j Number of jobs running in the current shell session.


\l Name of the current terminal device.


\n A newline character.


\r A carriage return.


\s Name of the shell program.


\t Current time in 24 hour hours:minutes:seconds format.


\T Current time in 12 hour format.


\@ Current time in 12 hour AM/PM format.


\A Current time in 24 hour hours:minutes format.


\u username of the current user.


\v Version number of the shell.


\V Version and release numbers of the shell.


\w Name of the current working directory.


\W Last part of the current working directory name.


\! History number of the current command.


\# Number of commands entered during this shell session.


\$ This displays a “$” character unless we have superuser privileges.

In that case, it displays a “#” instead.


\[ Signals the start of a series of one or more non-printing characters.

This is used to embed non-printing control characters which manipulate the terminal emulator in some way, such as moving the



cursor or changing text colors.


\] Signals the end of a non-printing character sequence.



Top OS Cloud Computing at OnWorks: