1751968 Members
4675 Online
108783 Solutions
New Discussion юеВ

changing the prompt

 
SOLVED
Go to solution
Allan Pincus
Frequent Advisor

changing the prompt

Can anyone give me a short script to put in my .profile so the last one or two arugements from PWD change when you execute the cd command.

I can manipulate the PS1 with static info, like hostnames, home dirs, whatever, but I can't seem to figure out how to dynamically change PS1 with each entry of cd.

And I don't want the WHOLE pwd (god forbid!!) just the last one or two fields, maybe preceeding with '.' or something.

Any ideas would be helpful!!

- Allan
14 REPLIES 14
Sajid_1
Honored Contributor

Re: changing the prompt

Hello,

Include this at the last of your profile file:

host=`hostname`
PS1='$host:$PWD># '

gl,
learn unix ..
Allan Pincus
Frequent Advisor

Re: changing the prompt

Sajid,

Thanks! That one I knew, but I only wanted the last couple of PWD args to show up. If you go down, say, 6 or 7 sub-dirs, you wind up with a big mess of a prompt.

I know you can truncate the PWD, I just don't know how it gets dynamically truncated.

- Allan
Arockia Jegan
Trusted Contributor

Re: changing the prompt

Use this

export PS1='${PWD}'

or


export PS1=`uname -n`':${PWD} # '
Hai Nguyen_1
Honored Contributor

Re: changing the prompt

Allan,

This shoudl work for you. It shows the current directory you are in.

# PS1="`hostname`:`basename $PWD` "

Hai
Hai Nguyen_1
Honored Contributor

Re: changing the prompt

Allan,

I add a pound sign to separate the prompt from commands.

# PS1="`hostname`:`basename $PWD` # "

Hai
Tom Maloy
Respected Contributor

Re: changing the prompt

Allan,

I prefer to have a multi-line PS1, so I can see the whole path:

systemname=`uname -n`
username=`whoami`
PS1='\[ $username - $PWD \] $systemname -> '

Which gives something like this:

[ myname - /home/myname ]
systemname -> cd /var/adm/syslog
[ myname - /var/adm/syslog ]
systemname ->

So I can see who I am currently logged in as, where I am, and what system I am on. This has saved me far too often from running a command in the wrong directory on the wrong system.

Tom
Carpe diem!
Tom Maloy
Respected Contributor

Re: changing the prompt

Oops. A line continuation character was "eaten" - PS1 should be TWO lines:

PS1='\[ $username - $PWD \] \

$systemname -> '

At the end of the PS1 line, put a line continuation character (/) and then hit Enter to put $systemname... on the next line.

Carpe diem!
Allan Pincus
Frequent Advisor

Re: changing the prompt

Hi all,

Thanks for the QUICK (and I mean QUICK) responses. This is a super news group.

So far, I haven't gotten the nail on the head yet.

"basename" comes closest. I tried this myself, but for some reason `basename $PWD` returns only a static representation of the path. For example, suppose your current PWD is:

/a/b/c/d

and you do: export PS1="`basename $PWD` > " you get:

d > Which is what I was trying to do. So far, so good. But then:

cd ..

d > pwd
/a/b/c

I was hoping to see:

c > pwd
/a/b/c

I haven't gotten it yet!

- Allan
Tom Maloy
Respected Contributor
Solution

Re: changing the prompt

If you are using ksh, this will work:

PS1='${PWD##*/} '

##*/ removes the large left pattern, leaving only the equivalent of "basename $PWD". Note that this must be in single quotes to work correctly.

Tom
Carpe diem!