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
required
\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.
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
Esther Schindler
If the comments are ugly, the code is ugly
claird
SVG a graphics format for 21st century
pasmith
Take Chrome OS for a test spin
Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?
jfruh
Android fragments vs. the iPhone monolith
mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.














unix prompt
I have been using the same prompt for quite a few years now.It tells me current directory, time, tty and which oracle database has been used to set the oracle environment:
function go {
\cd $1
typeset -Z2 _h; typeset -Z2 _m # 2 digits, zero padded
_hh="(SECONDS/3600)%24" _mm="(SECONDS/60)%60" # hours, minutes
_time='${_x[(_m=_mm)==(_h=_hh)]}$_h:$_m'
typeset bold=$(tput bold)
typeset shy=$(tput rmso)
PS1="[ `pwd` ]"
PS1=$PS1`echo "\n\n${bold}$_time-${UMACHINE}:${ORACLE_SID}${shy}:${LOGNAME}-$TTY > "`
}
alias cd='go '
Here's an example:
[ /home/jared ]
12:42-ordevdb01:dv11:jared-18 >
It works in both ksh and bash.
I should probably update to take advantage of some of the newer features in both ksh and bash.
why that error disinterest?
Nice article, with one major omission:Can anyone clue me in on just why everyone is so disinterested in $? ?
Looks like this must be an experience to look forward to:
* To erase some files, cd somewere, press enter, type rm *, press enter.
* Hmmm. Oops. I mistype somewhere. somewere of course does not exist.
* Hmmm? OOOPS. Didn't I have a backup of those file I just accidentally deleted? Ah here.
* WHAT!!!? only from last year?
==
How about something like this one for bash and ksh93:
PROMPTCHAR="%";
PROMPTPREFIX="#";
[ "$UID" -lt "${MAXSYSTEMUSERUID:-1000}" ] && PROMPTCHAR="#";
[ "$UID" = "0" ] && PROMPTCHAR="##" && PROMPTPREFIX="## ";
PS1=$PROMPTPREFIX'($?) ${debian_chroot:+($debian_chroot) }'
# linebreak after $PWD
PS1="$PS1"'`date +%H:%M:%S` $LOGNAME@$HOSTNAME $PWD
'$PROMPTPREFIX'$SHELL$SHNEST !'$PROMPTCHAR' '
# or maybe a more efficient one for just bash
PS1="$PROMPTPREFIX(\$?)"
PS1="$PS1 ${debian_chroot:+($debian_chroot) }"
PS1="$PS1\\t \\u@\\h \\w\n"
PS1="$PS1$PROMPTPREFIX\\s$SHNEST $PROMPTCHAR "
# note I did split the assignment into multiple to hopefully avoid extra faux linebreaks from html formatting.
cu
Peter
Shell prompts
These two functions, written for the Korn shell, replace the user's home dir with ~ and contract the path if it's deemed to be too long and show the time when you change directory, rather than clutter up the prompt with it.Pwd() {
mypwd=$( /usr/bin/pwd | /usr/bin/sed \
-e "s%/$HOME/%~/%")
if [ "$mypwd" = '/' ] ; then # expr barfs if var is just '/'!
echo $mypwd
else
len=$( expr $mypwd : '.*' )
if [ 30 -lt $len ] ; then
a=$( expr $mypwd : '\([/~][^/]*/\)' )
b=$( expr $mypwd : '.*\(/[^/]*\)$' )
echo "$a...$b"
else
echo $mypwd
fi
fi
}
Cd() {
\cd $1 $2 >/dev/null
[ "${dirs%% *}" = "$PWD" ] || push $PWD # Avoid duplicates.
print "$(date +%H:%M)"
PS1="[\!]($me@$host)$(Pwd): "
}