1752728 Members
5568 Online
108789 Solutions
New Discussion юеВ

PS1 Confusion

 
SOLVED
Go to solution
Monte Heeren
Frequent Advisor

PS1 Confusion

Trying to figure out what this PS1 command is doing:
export PS1="$accountName "'${PWD##*($HOME|$HOME/)}$ '

The prompt looks like this:
[aea11-un] data$

The account name is "aea11-un"
The PWD is set to /ifastest/bsi/data
The $HOME is set to /ifastest/bsi
I'm confused about the two ## and the *. What do they do?
And the $HOME|$HOME/ ? Confused on what it does.
Looks like it is stripping off the 1st two nodes of the pwd command.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: PS1 Confusion

Hi Monte:

> I'm confused about the two ## and the *. What do they do?

Just what you deduced. The '${parameter##pattern}' is described in the 'sh-posix' manpages. If the pattern matches the beginning of the value of parameter, the value of this substitution is the value of the parameter with the matched portion deleted.

See the Pattern Substitution section of:

http://www.docs.hp.com/en/B2355-60105/sh-posix.1.html

> And the $HOME|$HOME/ ? Confused on what it does.

This says to match either the value of '$HOME' or ('|') the value of '$HOME/' (that is with a forward slash following it.

Regards!

...JRF...
Tingli
Esteemed Contributor

Re: PS1 Confusion

${PWD##*($HOME|$HOME/)}removes the full path of the user home directory.
Monte Heeren
Frequent Advisor

Re: PS1 Confusion

Thats exactly the answer I was looking for!
Your the best!
And thanks for the link to the HP docs section on pattern substitution. That paper has cleared up a lot of other questions I had.
This forum is priceless!
Bill Hassell
Honored Contributor

Re: PS1 Confusion

You might find this PS1 prompt a bit more useful:

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

In this example, the lowest two directories are shown. So if you change to /usr/local/bin, the prompt will be:

[aea11-un] local/bin $

Knowing that you are in the bin directory is not enough. The current PWD and the parent provide most of the details without showing the entire $PWD.

Further details: The double quotes around "$accountName " will be set permanently with PS1= assignment but the single quotes (apostrophes) will be evaluated every time the PS1 variable is displayed.


Bill Hassell, sysadmin
Monte Heeren
Frequent Advisor

Re: PS1 Confusion

Bill,
Thank you for the update. Never knew the PS1 prompt had sooo many options. I'll try your recommendation.