Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple doubt in PS1 variable 2

Status
Not open for further replies.

jehad

MIS
Jan 13, 2001
2
US
Hi,
It is mentioned that, to set the PS1 variable to show the present working directory PS1="\$pwd >" is enough. Can any one explain what is the back slash in that command does.

Thanks,

Jed.
 
Hi Jed,

You need, I think, to set PS like this:

[tt]export PS1="\$PWD"[/tt]

The difference is that pwd is uppercased in my example.

The backslash is interesting, they always are actually.

If you try the command without the backslash it seems to work and your prompt is set to your current directory. But if you *change* directory the prompt doesnt change; for that you need the backslash.

In the shell a backslash means "the next character is *not* special". So when $PS1 is evaluated by the shell (each time a prompt is displayed) it evaluates the string '$PWD' which is a variable maintained by the shell to always contain your currrent directory. (without the backslash the evaluation is done only once, when you set $PS1)

Mike
michael.j.lacey@ntlworld.com
 
If you are using the bash you might as well use export PS1="\w", where w stands for the working directory.
regards
chenn
 
When you set your PS1 to PWD is ok.
But your prompt can become like this:
/opt/graphics/common/newconfig/opt/graphics/common/10.0_Rel_Notes>
It leaves you little space to type.

With this method:

CD(){
cd $1
PS1=$(basename $(dirname $PWD))/$(basename $PWD)
if [ -d /$PS1 ] ;then
PS1=$PWD
else
PS1=.../$PS1
fi
PS1="[$(hostname):$PS1]# "
}
alias cd='CD'


Your prompt will look like:
[Host02:.../common/config]#

When you have only one machine you can skip the hostname.

I use the HP10.20 /usr/bin/sh posix .
I placed the alias and function in the .kshrc so it will only
run at startup of the shell.

Mike thanks. I did not know that. Gregor.Weertman@mailcity.com
 
The "not enough space left" problem is a pain I know. I set mine like this:
[tt]
export PS1="$(hostname):\$PWD
>"
[/tt]

This leaves you with a complete line to type on but *does* take up two lines for a prompt - which some ppl don't like. I always wondered how the path truncation thing was done by the way - thanks. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top