Variables and Commands in Prompts
Customizing the Prompt
The command promptyou get
when you connect to a command line session is highly
customizable. I have used a simplified prompt of just a dollar
sign ($) in the examples in this book, but the default prompt in
Linux includes the user name, host name and the current directory.
[jemmons@oelinux-test1
u01]$
The prompt is set by defining the PS1 shell variable. If you
want the prompt to just contain text, simply define it just like
any other variable.
$ PS1=:
:PS1=': '
: PS1='Enter a command: '
Enter a command: PS1=$
$PS1='$ '
$
Changes to the PS1 variable
are seen immediately in the current session. In the example
above, first set the prompt to a colon (:). It is seen on the
next line that with just the colon, your prompt is not separated
from your command. To make the prompt more obviously separate
from the command, set PS1 to a colon with a space after it. To
set a variable to a value with a space in it, enclose the value
between single or double quotes. The space makes it a bit more
clear where the prompt ends and the command begins.
Next, change the prompt back to the familiar dollar sign but
now notice that the default prompt is not just a dollar sign, but
rather, a dollar sign followed by a space, so set PS1 to reflect
that. When you get to a prompt you are happy with, put the
definition of it in your .bash_profile file with other alias
definitions. Now every time you log in, your prompt will
automatically be set and ready to go.
Special Prompt Characters
Bash allows several special characters
to be used in a user's prompt which will then be expanded when the
prompt is printed. These characters can be used in combination
with text in the prompt, but always begin with a backslash (\).
Here are a few of the more useful special characters:
Special Character |
Description |
\d |
The date spelled out in the form of 'Weekday
Month Date'?? |
\@ |
The current time in 12 hour format with AM/PM |
\u |
The username of the current user |
\h |
The hostname of the system you are on |
\w |
The current working directory |
Table 8.1: Sample of Special Prompt Characters
Here are a few examples of how these special characters can be
used in a prompt. Note that the prompt following the change
represents the results of that change.
$ PS1='\d \@ $
'
Thu Oct 12 04:34 PM $ PS1='\u@\h$ '
oracle@ oelinux-test1$ PS1='\w $ '
~ $ cd /usr/local
/usr/local $
The change directory (cd) command
in this example is used to demonstrate that the \w character is
showing the user's present working directory. Commands and shell
variables can also be used in command prompts.
Variables and Commands in Prompts
Almost anything can be put in your prompt with shell variables
and commands. Oracle administrators often want their current
ORACLE_SIDlisted in their
command prompt. This is easily accomplished:
$ PS1='$ORACLE_SID
$ '
oss $
Be sure to use single quotes when enclosing variables and
commands within the prompt. If double quotes are used, any
variables or commands are interpreted and substituted when the
prompt is set but are not updated if the value changes during the
session. By using single quotes, the variables and commands are
substituted when the prompt is displayed. If, for instance, the
ORACLE_SID variable is changed to connect to a different database,
the new value for ORACLE_SID is reflected in the prompt.
To use commands in the prompt, they must be enclosed between
two grave symbols (`), more commonly called backquote or backtick
in UNIX circles. Here is an example:
$ PS1='`uptime
| cut -f 3-5 -d ","`$ '
load average: 0.03, 0.04, 0.01$
Here the PS1 prompt is set to show the current load average of the
system by using the uptime and cut commands. Everything within
the back quotes is evaluated and the result substituted in the
prompt. Then $ is added to make it clear where the prompt ends.
I suggest keeping your prompt simple. Remember, whatever you
put in there will be executed over and over while you are on the
system. Most of the time, I find username and hostname and
perhaps ORACLE_SID to be sufficient.
$ PS1='\u@\h
$ORACLE_SID $ '
oracle@ oelinux-test1 oss $
A secondary prompt is used when entering commands on multiple
lines. This PS2 promptis seen
when a backslash (\) is entered to continue a command on the next
line. The default is usually good for this since you do not see
it much, but if you wish to change it, set the PS2 environmental
variable in the same way you would set PS1.
Prompts PS3 and PS4 can also be set but these are rarely seen.
PS3 is used only in very specific shell syntax and PS4 is used
when using a special trace option within the shell.
Conclusion
A handful of meaningful customizations can make the Linux
environment easier to use and more flexible. Changes to the login
shell, variables and binary search path may sometimes be necessary
when setting up new software.
It is important to remember that many of these customizations
change the way the shell works and can affect how software runs.
Changes to the PATH variable in particular can have unexpected
consequences.
The next chapter will examine the use of the at and cron
utilities when scheduling tasks.