1833726 Members
2665 Online
110063 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!
Sean OB_1
Honored Contributor

Re: changing the prompt

I use the following:

me=`who am i | cut -d' ' -f1`
host=`hostname`
PS1='me@$host:$PWD:# '


Allan Pincus
Frequent Advisor

Re: changing the prompt

Tom:

You got it, right on the money! Exactly what I was looking for. I formatted your expression even prettier like this:

export PS1=`hostname`:./'${PWD##*/} > '

Which returns something like this:

myServer:./lastPath > _

And it changes with each cd command.

Cool!

One question:

I've never seen the ##*/ operator, or (or is this a grep expression?) Can you explain how this is working?

Thanks a bunch!!

- Allan
A. Clay Stephenson
Acclaimed Contributor

Re: changing the prompt

If you do a man sh_posix and look under the 'Parameter Substitution' section, you will see just how this is working as well as a few other ways to use substitution.
If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

Re: changing the prompt

This is probably what you are looking for:

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

Be sure you use single quotes. This will show the current directory and one level up. It's really useful for a short prompt but with a little less ambiguity than just the basename (which bin directory is it?)

BTW: The # and % constructs are specific to POSIX shells such as ksh, bash and the HP POSIX shell. It has to do with pattern matching (shortest or longest, left or right pattern). It's a bit difficult to grasp from the man pages; try a good Korn shell book.


Bill Hassell, sysadmin
Kenneth_18
Frequent Advisor

Re: changing the prompt

Just a suggestion. How about using the bash shell? PS1="\u@\h: \W \\$ " will give you the PS1 prompt of a typical linux prompt like

username@hostname current_working_directory root/non-root sysmbol.

for root a root user, it would look like similar to this:

root@hostname: / #

for a non-root user it would look similar to this:

user1@hostname: user1 $