Changing text in the command prompt

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.

You have never mentioned the shell that you are using! :slight_smile:

Assuming zsh,

set this,

PS1=[%n:%d]

username ==> n
current dir ==> d

I'm using the Korn shell ksh.
Thanks.

try this,

PS1=$USER:$PWD:

There is no environment variable with the name USER.
Initially the command prompt is set correctly. But once I change the directory using the cd command, the current directory is not being reflected in the path.

I want my command prompt to always contain the current user and the current directory in it.

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.

You can try the following setting :

export PS1="`whoami`\$PWD>"

duplicate post ?

"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 !

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.

Put this in your .kshrc

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