Changing text in the command prompt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing text in the command prompt
# 8  
Old 03-07-2007
# 9  
Old 03-07-2007
Quote:
Originally Posted by unipepper
Hi,
I want to change my command prompt to contain the current username and the current directory in it, instead of just the '$' symbol.

I tried the command:-
export PS1="[$(whoami) $(pwd)]$(echo \\n$) "

But whenever I switch the user or change the directory, the changes are not reflected in the command prompt. Could anyone pls help me?

Thanks in advance.
"cd" is a build-in command so you will need to create a wrapper around "cd" in order to achieve it - which means u need to "alias" cd.

So, something like this "may" solve your problem (thats what I thought atleast)
alias cd='cd \!*; export PS1="[$(whoami) $(pwd)]$(echo \\n$) "'

But the issue is you cannot pass arguments in k-shell so, "cd \!*" wouldn't work.

This would be possible in csh I guess.

You will need to enclose this in a function and then alias "cd" to the function name:

So, in your .profile do this like:

------
export PS1="[$(whoami) $(pwd)]$(echo \\n$) "

function newcd {
"cd" "$@"
ret=$?
export PS1="[$(whoami) $(pwd)]$(echo \\n$) "
return $ret
}

alias cd=newcd
------
Enjoy !

Last edited by Deal_NoDeal; 03-07-2007 at 12:42 PM..
# 10  
Old 03-07-2007
Quote:
Originally Posted by unipepper
I'm using the Korn shell ksh.
Which version of ksh? There are at least 3 major versions of ksh. Press Ctrl-V for the version.

That said, this will work in most shells:

PS1='$USER $PWD
$ '

Note that the value is enclosed in single quotes so that the variables are expanded when the prompt is printed, not when they are assigned to PS1.
# 11  
Old 03-07-2007
Put this in your .kshrc

Code:
HBRI="`tput bold`"
INV="`tput rev`"
NRM="`tput sgr0`"
UNAME=`whoami`
PS1="$HBRI$INV$UNAME:$NRM$INV\${PWD#\${PWD%?/*/*/*}??}\>$NRM"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing default prompt

Hi, i was wondering if it is possible to change the default prompt for the shells that are availble on your system? For example, i want to change the prompt for the C shell from % to something like ( or ). Thanks, Nav. (7 Replies)
Discussion started by: Navs_
7 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Help needed to have changing value to the command prompt string variable PS1

Hi, I am using git bash terminal window to do git operations. I have set the prompt string variable PS1 in the ~/.bashrc file as follows: export PS1=" " This is intended to show me the current git branch's name which is active as part of the prompt string. But, the problem is when I do a git... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

Changing the shell prompt

Hi, I want to change the shell prompt, using the cd command. I have a shell prompt like this - p78-mfx(dgaw1078/9781)$ Now i do this - p78-mfx(dgaw1078/9781)$ cd log4j here the shell prompt should change like this - p78-mfx(dgaw1078/9781)log4j$ (6 Replies)
Discussion started by: arunkumarmc
6 Replies

4. UNIX for Advanced & Expert Users

Changing the password prompt

Is there any way I can change the prompt which asks for the password on a UNIX system? e.g. When I login using Telnet instead of "Password" I should get "Correct Password". Thanks, Vineet (3 Replies)
Discussion started by: vineetd
3 Replies

5. Shell Programming and Scripting

problem in changing prompt

hi all, please tell me why this 2 liner script is not working!! #!/bin/bash oldps1="$PS1" PS1="myprompt>" but when type this in terminal it works!! (7 Replies)
Discussion started by: tprayush
7 Replies

6. Solaris

Changing prompt

I currently have this as my prompt when I log in (shell is sh): PS1="`hostname ` # " My question is how do I add the current directory to that prompt? Is there a way? Thanks. (5 Replies)
Discussion started by: kjbaumann
5 Replies

7. UNIX for Dummies Questions & Answers

Changing Prompt in sh

I know no one has ever asked this before {not :D } but I am trying to set the prompt in the .profile under sh. I have tried everything I have seen on the web in regards to this, with no success. The OS is SCO Unixware 7.1.1, {not by my choice}. All the examples I see seem to be for ksh, which is... (1 Reply)
Discussion started by: jcc5169
1 Replies

8. UNIX for Dummies Questions & Answers

Changing the prompt

Phew simple question, I want to display the my directory path in prompt. Did the following in .profile PS1=`pwd` export PS! Worked, but it always points to HOME directory. When i do a cd, it doesn't change. What am i missing. Thanks (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

9. UNIX for Dummies Questions & Answers

Changing Unix Prompt

Me again, What is the difficulty to display the full directory Path before my prompt command ? (like DOS) I'm using Solaris 8 + Bash Thanks again Fabien (4 Replies)
Discussion started by: unclefab
4 Replies

10. UNIX for Dummies Questions & Answers

Changing the UNIX command prompt

I am having a hard time figuring out how to change the command prompt in my UNIX shell. I am using the bash shell, and I would like to set the prompt to show me the full path of the current working directory along with my username, I suppose... The main thing I want is the full path of the... (2 Replies)
Discussion started by: WERUreo
2 Replies
Login or Register to Ask a Question