Help changing the PS1 prompt in Solaris

Hi, I need help changing PS1 in Solaris. I tried this:

MYPROMPT="> "

PS1=$LOGNAME@$HOSTNAME:${PWD}$MYPROMPT (NOT SURE WHY IT'S HIGHLIGHTED HERE)

export PS1

My problem is that $PWD is not working, when I get the prompt and I change directories, the prompt is not displaying the current (changed) directory. I've seen other posts where people do single quotes:

PS1='$LOGNAME@$HOSTNAME:${PWD}$MYPROMPT'

but that didn't work for me as I get this:

$LOGNAME@$HOSTNAME:${PWD}$MYPROMPT

as the prompt !!!.

Please help.

Thanks.

FYI - for bash:

export PS1="[\$USER@\$(hostname):\$PWD] "

Just stop using /bin/sh as a login shell and switch to ksh or bash.

By the way, I would also recommend to avoid the final "> ".

Standard bourne shell prompt are usually ending with "$" for casual users and "#" for root. Sticking to that standard helps knowing who you are running command as. Moreover, "> " is risky as a terminal window copy/paste "accident" is less likely to create unwanted files, or worst, clear existing ones.

vgersh99: I'm using Korn shell, and your suggestion / command didn't work. Any ideas/code for the Korn shell (Solaris box).

SunOS kdsan004 5.10 Generic_150400-09 sun4u sparc SUNW,SPARC-Enterprise

jlliagre: I am using the Korn shell (/bin/ksh).

If BASH what you probably want is:

PS1="\u@\h:\w> "

Dont forget to export it if you like it.

hth

I'm afraid you are not.

This behavior can only be observed with the legacy bourne shell. All other Bourne shell style shells available under Solaris, including /bin/ksh, /bin/bash, /usr/xpg4/bin/sh, and /usr/dt/bin/dtksh will properly handle this PS1 setting (but not sea's suggestion which is bash specific).

1 Like

jlliagre: Yes I am:

 twsapps@kdsan004:/opt/apps/twsapps> echo $SHELL
/bin/ksh

Any other thoughts ?

Thanks.

Hmm, you were complaining about getting this prompt in your initial question:

$LOGNAME@$HOSTNAME:${PWD}$MYPROMPT

but you are now showing this quite different prompt shows up:

twsapps@kdsan004:/opt/apps/twsapps>

Can you please clarify what is not working?

jlliagre:

First of all, thank you for your willingness to help. You're very kind.
So, I got this prompt: twsapps@kdsan004:/opt/apps/twsapps> by defining PS1 in the .profile as :

PS1=$LOGNAME@$HOSTNAME1:$%7BPWD%7D$MYPROMPT"PS1=$LOGNAME@$HOSTNAME1:${PWD}$MYPROMPT

(MYPROMPT was previously defined as : MYPROMPT="> " .

Here's my dilemma: When I'm at the prompt and I type, let's say: cd mydir , the prompt should change to

twsapps@kdsan004:/opt/apps/twsapps/mydir> 

, meaning, it should display the current directory, which now is

twsapps@kdsan004:/opt/apps/twsapps/mydir

, but instead, it stays as

twsapps@kdsan004:/opt/apps/twsapps>

, which means PWD is not getting updated/refreshed.
I hope the problem is now clear.
Thanks again.

Have you tried what'd been suggest previously?

export PS1="[\$USER@\$(hostname):\$PWD] "

Also when you're at the shell prompt, provide the output of echo $0

Put this in .profile:

export MYPROMPT="> "
[ -z "$HOSTNAME" ] && export HOSTNAME=`/bin/uname -n | /usr/bin/cut -f1 -d.`
export PS1='$LOGNAME@${HOSTNAME}:${PWD}$MYPROMPT'

Vgersh99:

I changed PS1 to:

PS1="[\$USER@\$(hostname):\$PWD] "
export PS1

and I got this:

[$USER@$(hostname):$PWD] pwd
/opt/apps/twsapps
[$USER@$(hostname):$PWD] cd TWA
[$USER@$(hostname):$PWD] pwd
/opt/apps/twsapps/TWA

So it didn't work. Thanks though, and any other thoughts are welcome.

---------- Post updated at 03:55 PM ---------- Previous update was at 03:49 PM ----------

vgersh99: I forgot to show you this:

twsapps@kdsan004:/opt/apps/twsapps> echo $0
-sh

sh != ksh (at least on Solaris).

MadeinGermany:

When I typed your suggestion, I get :

 HOSTNAME=kdsan004: is not an identifier

Also, if I type anything in ' ', I get the same stuff within the single quotes as a prompt:

 $LOGNAME@${HOSTNAME}:${PWD}$MYPROMPT

---------- Post updated at 04:11 PM ---------- Previous update was at 04:06 PM ----------

vgersh99: But at the beginning of my code I have:

 #!/bin/ksh (first line)

I even have this:

 SHELL=/bin/ksh
export SHELL

also, at the command line:

twsapps@kdsan004:/opt/apps/twsapps> echo $SHELL
/bin/ksh

finally, I checked the /etc/passwd file:

twsapps@kdsan004:/opt/apps/twsapps> more /etc/passwd |grep twsapps
twsapps:x:1200001858:20002:Service account for twsapps #cust# RM13600277:/opt/apps/twsapps:/bin/sh

Could that be the problem ? If so, how do I change the shell within the script, w/o changing the /etc/passwd file or is that absolutely necessary ?

Thanks.

PS1='${PWD}'"$ "

it is not Solaris, but ksh.

Yes, echo $0 or ps -p $$ tell you the truth.
.profile is run by the login shell that is

getent passwd $LOGNAME

SHELL is just an environment variable that is set by the login shell.

---------- Post updated at 04:18 PM ---------- Previous update was at 04:10 PM ----------

You can try to change your login shell with

passwd -e

--
Another proposal: have the following in .profile

ENV=$HOME/.kshrc; export ENV

And put the ksh stuff in $HOME/.kshrc file, so that

cat $HOME/.kshrc
export MYPROMPT="> "
[ -z "$HOSTNAME" ] && export HOSTNAME=`/bin/uname -n | /usr/bin/cut -f1 -d.`
export PS1='$LOGNAME@${HOSTNAME}:${PWD}$MYPROMPT'

You can switch your current shell to ksh with

exec ksh

Too bad you stubbornly refused to accept you were running /bin/sh in the first place while that was the only reasonable explanation.

Are you talking about your .profile or something else?

now Solaris:

# uname
SunOS
# cat /etc/release
                   Oracle Solaris 10 8/11 s10s_u10wos_17b SPARC
  Copyright (c) 1983, 2011, Oracle and/or its affiliates. All rights reserved.
                            Assembled 23 August 2011

                  Solaris 10 1/13 (Update 11) Patchset applied.
# PS1='${PWD}'" $ "
/export/home/user $ cd ..
/export/home $
/export/home $ echo $SHELL
/bin/ksh