Skip to content

UNIX Interactive Prompts

February 29, 2012

Generic UNIX Interactive Prompts

The following Korn/POSIX, BASH, C, and TurboC Shell prompts are for displaying the user’s hostname and present working directory. Be careful to use the exact characters, quotes, and syntax.

See the last section for instructions on how to invoke shell environment changes without having to log out and back in again.

The venerable Bourne shell, from which the Korn and POSIX shells are descended, does not provide for interactive prompts.

C (CSH) and TurboC (TCSH) Shells

Place the following into the .cshrc or .tcshrc file:

alias setprompt 'set prompt="`whoami`@`hostname`:($cwd)":""" "'  
alias cd 'cd \!* && setprompt'
alias pushd 'pushd \!* && setprompt'
alias popd 'popd \!* && setprompt'
setprompt

The resulting prompt would look like this:

        wlsoem@malsrvdev03:(/home/web/wlsoem): 

Korn (KSH) and POSIX (SH) Shells

The Korn shell requires that the following lines be placed into the .profile file located in the user’s home directory to invoke a secondary ENVIRONMENT file:

 
ENV=~/.kshrc
export ENV

The POSIX shell requires that the following lines be placed into the .profile file located in the user’s home directory to invoke a secondary ENVIRONMENT file:

 
ENV=~/.shrc
export ENV

Prompt Type 1: Place the following into the .kshrc or .shrc file:

 
HOST=$(uname -n)
export HOST
PS1='$HOST:$PWD $'
export PS1

OR
export PS1=`uname -n`:'$PWD':" "

The resulting prompt would look like this:

ora12crac1:/export/home/oracle:

Prompt Type 2: Place the following into the .kshrc or .shrc file (This one contains an embedded carriage return for keeping the command line clear when deep into subdirectories):

 
HOST=$(uname -n)
export HOST
PS1='$HOST:$PWD $
'
export PS1

The resulting prompt would look like this:

mars:/export/home/joe/bin/original/programming/development/secret/stuff
$

Bourne-Again Shell (BASH)

Prompt Type 1: Place the following into the .bashrc file:

 
PS1='\h:$PWD $'
export PS1

The resulting prompt would look like this:

mars:/export/home/joe $

Prompt Type 2: This one contains an ASCII “new line” feed, which has the effect of an embedded carriage return for keeping the command line clear when deep into subdirectories. Place the following into the .bashrc file:

 
PS1='\h:$PWD \n$'
export PS1

The resulting prompt would look like this:

mars:/export/home/joe/bin/original/programming/development/secret/stuff
$

Further BASH Prompt Customization

The BASH allows prompt string customization by using these backslash-escaped characters:

 
\a     an ASCII bell character (07)
\d     the  date  in  "Weekday  Month  Date" format (e.g., "Tue May 26")
\e     an ASCII escape character (033)
\h     the hostname up to the first `.'
\H     the hostname
\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
\u     the username of the current user
\v     the version of bash (e.g., 2.00)
\V     the  release  of  bash, version + patchlevel (e.g., 2.00.0)
\w     the current working directory
\W     the basename of the current  working  directory
\!     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
\]     end a sequence of non_printing characters

Therefore, ASCII escape characters could be used to create a colourful BASH prompt and include an embedded carriage return:

PS1="\[33[1;32m\]\h33[0;30m\]:\[33[1;36m\]\u33[0;30m\]:33[1;31m\]\$PWD\[33[0;30m\]\n$ "
export PS1

The resulting BASH prompt would look like this:

mars:user1:/home/user1
$

Here are the rest of the colour equivalences:

       Black       0;30     Dark Gray     1;30
       Blue        0;34     Light Blue    1;34
       Green       0;32     Light Green   1;32
       Cyan        0;36     Light Cyan    1;36
       Red         0;31     Light Red     1;31
       Purple      0;35     Light Purple  1;35
       Brown       0;33     Yellow        1;33
       Light Gray  0;37     White         1;37

Restarting the Shell Environment After Changes

With all shells, remember to source (restart) your environment files after making any changes in them (i.e. .profile, .kshrc, .shrc, .bashrc, .cshrc, .tcshrc, .login).

BOURNE, KSH, POSIX

The first step is to source/execute the .profile:

 
. .profile

This reads: dot dotprofile

The second step (except for the Bourne shell, which does not allow an ENVIRONMENT file) is to source the file assigned to the ENV variable:

 
. .shrc (POSIX)
. .kshrc (KSH)
BASH

The only step is to source the .bashrc:

 
. .bashrc
CSH and TCSH

These shells use the command source rather than a dot:

 
source .cshrc (CSH)
source .tcshrc (TCSH)
Advertisements

From → OS, Unix

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: