Unix Prompts for the Masses

One of the first things new users might want to know is how to change their prompts. It's a small change, but one that might make them feel more at home in their new working environment. Besides, it's so easy to do. Why not indulge them? I have worked on numerous systems that include both the system name and current working directory in the prompt -- and sometimes a command number too! If your prompt ends up looking like this, you might see why a new user might be a little frustrated with it:

armageddon:/export/home/willis/movies/scripts/part2 #117>

Maybe your user doesn't need to be reminded for every command that he types just where he is sitting in the file system and how many commands he has typed already without getting all that much accomplished. The proper prompt can help the new user to feel at home or, at least, give him the feeling that he has some control over his new environment. The wrong prompt can get on his nerves. Depending on the task at hand, some prompts can be considerably more useful than others!

In some situations, being reminded of where you are currently sitting in the file system is very useful. If you're working in a single directory, on the other hand, the constant reminders can be distracting and annoying and maybe some other piece of information or a single character prompt would be so much better.

Bash users will find that their options for prompt customization are quite generous. Here are some sample commands for prompt customizations:

PS1='> '		A simple and non-distracting >
PS1='$PWD $ '		Where am I?
PS1='$PWD : $LOGNAME >'	Where and who am I (lest they forget)?
PS1="\T> "		How time flies!
PS1="\d \h $ "		What's the date and what system am I logged into?
PS1='$USER@\h > '	Who am I and what system am I logged into?
PS1='what now?> '	The impatient system
PS1="try that again> "  The encouraging system
PS1="\#> "		I've already tried # commands?
PS1='\a> '		Making an audible sound

The variety of things you can insert into a command prompt when using bash is impressive. Any mix of these options can be plugged into the system prompt as shown in the examples above.

\a : an ASCII bell character (07)
\d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} : the format is passed to strftime(3) and the result is inserted into the prompt
     string; an empty format results in a locale-specific time representation. The braces are
\e : an ASCII escape character (033)
\h : the hostname up to the first '.'
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the basename of the shell’s terminal device name
\n : newline
\r : carriage return
\s : the name of the shell, the basename of $0 (the portion following the final slash)
\t : the current time in 24-hour HH:MM:SS format
\T : the current time in 12-hour HH:MM:SS format
\@ : the current time in 12-hour am/pm format
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
\! : the history number of this command
\# : the command number of this command
\$ : if the effective UID is 0, a #, otherwise a $
\nnn : the character corresponding to the octal number nnn
\\ : a backslash
\[ : begin a sequence of non-printing characters, which could be used to embed a terminal
     control sequence into the prompt
\] : end a sequence of non-printing characters

For your Korn shell users, you can easily stick environment variables and command output into their prompts as shown in the examples below.

PS1='$PWD> '				Where am I?
PS1="$LOGNAME@`uname -n` \$PWD ! $ "	Who and where am I?
PS1='${PGHOST} > '			What psql server am I using?
PS1='${TZ}> '				What timezone am I in?
PS1='!$ '				How many commands have I typed?
PS1="`date`> "				What date and time is it?

The PS1 prompt is the most obvious prompt to change, but you can also redefine PS2, PS3 and so on if you're so inclined. Most users are unlikely to get past PS1 and PS2, however, and are unlikely to care much about their prompts beyond PS1.

This article is published as part of the IDG Contributor Network. Want to Join?

Computerworld's IT Salary Survey 2017 results
Shop Tech Products at Amazon