Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Can't get this to work!' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Can't get this to work!
Authored by: clh on Dec 30, '01 09:11:16PM

I tried what the original post suggested, but my prompt does not dynamically change as I navigate directories.

Here's some debug info:

[chap@sudi:/Users/chap]$ echo $PROMPT_COMMAND
prompt_command
[chap@sudi:/Users/chap]$ prompt_command
[chap@sudi:/Users/chap]$ echo $newPWD
/Users/chap
[chap@sudi:/Users/chap]$ echo $PS1
[u@h:/Users/chap]$
[chap@sudi:/Users/chap]$ cd shex
[chap@sudi:/Users/chap]$ pwd
/Users/chap/shex

In .bashrc is the line

PS1="[u@h:$newPWD]$ "

So it appears that $newPWD is being substituted immediately at shell startup, rather than prior to displaying each prompt, the way w is. In order for this to work, I would think that PS1's value would have to remain "[u@h:$newPWD]$ " throughout the shell session.

Any thoughts?

PS - no clue how to make backslashes appear in the above - just assume it's there in front of u, w, h.



[ Reply to This | # ]
Correction to original post?
Authored by: clh on Dec 31, '01 02:26:54AM

For the solution in the original post to work, I believe you need to set PS1 *inside* the prompt_command function.

The final lines of the function should read

...
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then newPWD="$(echo -n $PWD | sed -e "s/.*(.{$pwd_length})/1/")"
else newPWD="$(echo -n $PWD)"
fi
PS1="[u@h:$newPWD]$ " ## <-- add this line to recompute PS1
}

Another solution, which doesn't require burying the assignment of PS1 inside a function, is to modify prompt_command as follows:

...
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then newPWD="$(echo -n $PWD | sed -e "s/.*(.{$pwd_length})/1/")"
else newPWD="$(echo -n $PWD)"
fi
echo $newPWD ## <-- "return" the newly-computed CWD
}

Then set PS1 as follows

PS1="u@h:$(prompt_command)$ " ## <-- invokes prompt_command and inserts CWD

You don't need to use the PROMPT_COMMAND variable with this method.

Well, I'm new to this forum and I haven't learned how achieve WYSIWYGedness, so I'm sure this will be missing scads of backslashes when you read it on the web. But anyway, I *think* the original post was not quite correct.

semicolon hyphen close-parenthesis



[ Reply to This | # ]
Saving PS1, don't change it :)
Authored by: cheako on Sep 15, '03 05:43:33AM

This is what I have...

echo $PROMPT_COMMAND | grep -qe 'How many characters' || {
PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND';
'}' {
# How many characters of the $PWD should be kept;
if [ "$lastPWD" != "$PWD" ];
then lastPWD="$PWD";
tmpPWD="$(echo -n "$PWD" |
sed -e '\''s:^'\''"$HOME"'\'':~:'\'')";
pwd_length=41;
pwd_prefixlength=11;
if [ $(echo -n "$tmpPWD" | wc -c) -gt $pwd_length ];
then newPWD="$(echo -n "$tmpPWD" |
sed -e '\''s:\(.\{'\''$pwd_prefixlength'\''\}\).*\(.\{'\''$((
$pwd_length - $pwd_prefixlength - 2 ))'\''\}\):\1..\2:'\'')";
else newPWD="$tmpPWD";
fi;
unset pwd_length pwd_prefixlength;
fi; # tmpPWD is not newPWD;
}'; # Out of singel quoted string hell.
# Export is not needed, but PS1 is and this will be used there.
export newPWD="$(echo -n "$PWD" | sed -e 's:^'"$HOME"':~:')"
# Force newPWD update.
lastPWD=..Yo..
}; # grep for 'How many characters' in PROMPT_COMMAND;

# All of this should allready be set to a good default, make them better.
[ "$PRO_UID" ] || PRO_UID=$(id -u)
if [ "$PRO_UID" -eq 0 ]
then PS1='\[\017\]\h:\[$newPWD\]\$ '
else PS1='\[\017\]\u@\h:\[$newPWD\]\$ '
fi
unset PRO_UID

# The \017 fixes the \016 bug :) echo -e '\016' for deatails :P



[ Reply to This | # ]
Correction to original post?
Authored by: clh on Dec 31, '01 04:16:36AM

For the solution in the original post to work, I believe you need to set PS1 *inside* the prompt_command function.

The final lines of the function should read

...
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then newPWD="$(echo -n $PWD | sed -e "s/.*(.{$pwd_length})/1/")"
else newPWD="$(echo -n $PWD)"
fi
PS1="[u@h:$newPWD]$ " ## <-- add this line to recompute PS1
}

Another solution, which doesn't require burying the assignment of PS1 inside a function, is to modify prompt_command as follows:

...
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then newPWD="$(echo -n $PWD | sed -e "s/.*(.{$pwd_length})/1/")"
else newPWD="$(echo -n $PWD)"
fi
echo $newPWD ## <-- "return" the newly-computed CWD
}

Then set PS1 as follows

PS1="u@h:$(prompt_command)$ " ## <-- invokes prompt_command and inserts CWD

You don't need to use the PROMPT_COMMAND variable with this method.

Well, I'm new to this forum and I haven't learned how achieve WYSIWYGedness, so I'm sure this will be missing scads of backslashes when you read it on the web. But anyway, I *think* the original post was not quite correct.

semicolon hyphen close-parenthesis



[ Reply to This | # ]
What Version of bash are you using?
Authored by: dlandrith on Jan 01, '02 12:11:17PM

With the exception of the export statement that contains the type, this function is exactly (cut and pasted from my profile) what I use and it works perfectly.

I am using bash version 2.05a. What version are you using?



[ Reply to This | # ]
What Version of bash are you using?
Authored by: clh on Jan 01, '02 03:03:48PM

GNU bash, version 2.04.0(1)-release

When you type 'echo $PS1' at the prompt, what does it show?



[ Reply to This | # ]
Missing backslash?
Authored by: clh on Jan 01, '02 03:27:55PM

I tried putting a backslash in front of ${newPWD} in the PS1 definition and it worked. Is that what you use? If so, the backslash got deleted by geeklog in your original post.



[ Reply to This | # ]
What Version of bash are you using?
Authored by: dlandrith on Jan 01, '02 12:16:25PM

With the exception of the export statement that contains the type, this function is exactly (cut and pasted from my profile) what I use and it works perfectly.

I am using bash version 2.05a. What version are you using?



[ Reply to This | # ]