If you’re like me you always intend to customize your UNIX or Linux prompt but never seem to find the time to look up the options and make the change. Well, to give you a jump start here’s an excerpt from my book Easy Linux Commands.

In order to eliminate the need to frequently issue the pwd command to determine the current working directory, many Linux users choose to display the working directory within the Linux command prompt. Some Linux administrators even provide this service for their users when they create their Linux accounts. If a user’s command prompt does not contain the working directory, the command prompt can be modified by changing the prompt string 1 (PS1) shell variable as demonstrated here:

$ PS1="[\u@\h \w]\\$ "
[tclark@appsvr /home/tclark]$

This example of setting the PS1 variable also adds the username (\u) and hostname (\h) to the prompt. This can be very useful if you frequently connect to different hosts and as different users.

In order to avoid having to modify the prompt at each login, the following line of code can be placed within the appropriate configuration file, such as .bash_profile for the bash shell, within the home directory. To do this you can use a text editor like vi to add the following line to your .bash_profile file:

export PS1="[\u@\h \w]\\$ "

Note: Files that begin with a period will not appear when you list the contents of a directory. To see these hidden files use the command ls –a

There are even more options which you can put into your PS1 prompt. While it’s nice to keep your prompt fairly short you may find some of the other options useful. The following table contains a list of values that can be displayed within the PS1 and/or PS2 prompt strings:

Symbol

Displayed Value

\!

History number of current command

\#

Command number of current command

\d

Current date

\h

Host name

\n

Newline

\s

Shell name

\t

Current time

\u

User name

\W

Current working directory

\w

Current working directory (full path)

As a bonus, here are a few of my favorite options for the PS1 prompt:

Prompt:
export PS1='\u@\h$ '
Result:
oracle@glonk$

Prompt:
export PS1='\w$ '
Result:
/usr/local$

Prompt:
export PS1='\d$ '
Result:
Mon Oct 30$

You may notice that all these have the dollar sign ($) in them which is typical of bash prompts. There is also a space after the dollar sign so you can easily tell where your prompt ends and your commands begin.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


linux, unix, system administration, sysadmin, bash, shell, unix shell, shell prompt