HOWTO: Change the Unix prompt and windows title.

The default user prompt on Unix is “$” and the root prompt is “#”. In this article you will find how you can change the prompt to display a lot of useful information. The same or other information can also be displayed in the windows title. The prompt is set by changing the PS1 environment setting.

Some examples:

1. Username in prompt
PS1=”$USER >”
Result: “user1 >”

2. Username and hostname in prompt
PS1=”$USER@$(hostname) >”
Result: “
<!–
var prefix = ‘ma’ + ‘il’ + ‘to’;
var path = ‘hr’ + ‘ef’ + ‘=’;
var addy39780 = ‘user1’ + ‘@’;
addy39780 = addy39780 + ‘host1’ + ‘.’ + ‘mydomain’ + ‘.’ + ‘com’;
document.write( ‘<a ‘ + path + ‘\” + prefix + ‘:’ + addy39780 + ‘\’>’ );
document.write( addy39780 );
document.write( ‘<\/a>’ );
//–>\n user1@host1.mydomain.com
<!–
document.write( ‘<span style=\’display: none;\’>’ );
//–>
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
<!–
document.write( ‘</’ );
document.write( ‘span>’ );
//–>
>”

If your hostname is the hostname including your domain name you can truncate the domain name.

3. Username with truncated hostname in prompt
PS1=”$USER@$(hostname| awk -F . ‘{print $1}’ ) >”
Result: “user1@host1 >”

3. Username, hostname and current path in prompt
PS1=$(echo “$USER@$(hostname| awk -F . ‘{print $1}’) “‘$PWD'” >”)
Result: “user1@host1 /home/user1 >”

4. Username, hostname, current Oracle SID and current path in prompt
PS1=$(echo “$USER@$(hostname| awk -F . ‘{print $1}’) “‘$ORACLE_SID'” “‘$PWD'” >”)
Result: “user1@host1 MainDB /home/user1 >”

By putting all this information in the prompt the prompt can get extremely long. In the next example all the information is on one line and the prompt on the next line.

5. Prompt on two lines
PS1=$(echo “\n[$USER@$(hostname| awk -F . ‘{print $1}’) “‘$ORACLE_SID'” “‘$PWD'”] $(echo “\n”)> “)
Result: [user1@host1 MainDB /home/user1 ]
>

All the information can also be set in the windows title. This only works on “terminals” that support this option. So in the profile you best test the “TERM” environment setting first. This option also works with putty ssh sessions.

Below a little script you can add to your personal profile or to your system profile.

The prompt will look like:
[ user1@host1 MainDB /home/user1 ]
$

The windows title will look like:
host1 as user1 {MainDB} <The first user> /home/user1

When this user switches to root the prompt will look like:
[ root@host1 / ]
$

The windows title will look like:
host1 as root {} <The first user> /

The name between <> is the real user name so it will always be “The first user” and will not change to the display name of root. Running a lot (putty) sessions to different Unix systems from windows you will get one grouped putty item. When clicking on this taskbar group you can quickly find the session you are looking for. The hostname is at the front of the windows title to make it easier to find the right session.

When activating this script on every Unix system in your environment you can telnet, rsh, ssh and “su” as much as you want, the information on the prompt AND in the windows title will always be accurate.

See Microsoft article kb#281628 (http://support.microsoft.com/kb/281628) to change the behavior of the grouping on windows. Changing the grouping setting to “2”, windows will always group all applications.

# Prompt settings
REALUSER=$(who am i |head -1 |awk ‘{print $1}’)
REALNAME=$(cat /etc/passwd |grep “^$REALUSER:” |awk -F ‘:’ ‘{print $5}’)
HOSTNAME=`hostname | awk -F . ‘{ print $1 }’`

if [ “$USER” = “root” ]
then
PS2=’# ‘
else
PS2=’$ ‘
fi

PS1=””
if [ “$TERM” = “xterm” -o “$TERM” = “dtterm” -o “$TERM” = “aixterm” ]
then
PS1=$(echo “33]0;$HOSTNAME as $USER {“‘$ORACLE_SID'”} <$REALNAME> “‘$PWD'”07”)
fi
PS1=$PS1$(echo “\n[ $USER@$HOSTNAME “‘$ORACLE_SID'” “‘$PWD'” ]$(echo “\n$PS2″)”)

export PS1


Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post
 |  TrackBack URI for this post


3 Responses to “HOWTO: Change the Unix prompt and windows title.”

  1. Elisha Sugalski says:

    Thanks for this gr8 article. I found it through yahoo and i find it very helpful. i will look for more interesting new articles at this homepage. (=

  2. Essie Monter says:

    Hello! this site of your blog was very interesting for me. But it was difficult to find it with yahoo. Maybe you should improve it with seo wordpress plugins for wordpress like headspace2. Just a tip 😉 (=

  3. Deals says:

    My developer is trying to persuade me to move to .
    net from PHP. I have always disliked the idea because
    of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on a variety of websites for about
    a year and am worried about switching to another platform.
    I have heard excellent things about blogengine.net. Is there a way I
    can transfer all my wordpress posts into it?

    Any help would be really appreciated!

Leave a Reply