Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bash prompt stuff

1 view
Skip to first unread message

juic...@mailinator.com

unread,
Jul 12, 2005, 6:04:42 PM7/12/05
to

hey, I'd like to customize my bash prompt a bit...

currently I have:
PS1="-\h:\w- \u$ "

the problem I have is sometimes I get pretty deep into my directory
structure, and some of my directory names are pretty long. Is there
some way to change the \w in my prompt to:
1) if the directory structure is less than 30 characters, just display
it
2) if the directory structure is more than 30 characters, change it to
display three dots (like an ellipsis) and the last 27 characters of the
path
for instance:
/some_really_long_path/going/to/some_place gets changed to
...ng_path/going/to/some_place

so the prompt would look like:
-hostname:...ng_path/going/to/some_place- username$

thanks in advance...

Chris F.A. Johnson

unread,
Jul 12, 2005, 6:26:04 PM7/12/05
to

PROMPT_COMMAND='if [ ${#PWD} -gt 30 ]
then PS1="-\h:...${PWD: -27}\u$ "
else PS1="-\h:\w- \u$ "
fi'

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>

Icarus Sparry

unread,
Jul 12, 2005, 7:32:24 PM7/12/05
to

If you have bash3, then the following will do what you want, after you put
in a few more question-marks.

PS1='-\h:${PWD/#??????????????*/...${PWD#${PWD%??????????????}}}- \u\$ '

The first set of question-marks determine the length where the prompt is
changed, and the second set determine how many characters of the name are
displayed.


Icarus Sparry

unread,
Jul 12, 2005, 7:40:02 PM7/12/05
to
On Tue, 12 Jul 2005 15:04:42 -0700, juic...@mailinator.com wrote:

If you have bash3, then the following will do what you want, after you put
in a few more question-marks, and change the number

PS1='-\h:${PWD/#??????????????*/...${PWD: -9}}- \u\$ '

The question-marks determine the length where the prompt is changed, and
the -9 determine how many characters of the name are displayed.

juic...@mailinator.com

unread,
Jul 13, 2005, 9:39:26 AM7/13/05
to

Thanks for all the responses guys...

I ended up going with Chris F.A. Johnson's solution. I like the idea
of having the PROMPT_COMMAND do all the checking 'work'. Thanks
Chris!

bsh

unread,
Jul 13, 2005, 6:50:47 PM7/13/05
to
juic...@mailinator.com wrote:
> hey, I'd like to customize my bash prompt a bit...

Let another script do the work for you...

http://www.dotfiles.com/files/3/183_.bash_prompt
http://bashish.sourceforge.net # "bashish.bash"
http://sunsite.bilkent.edu.tr/pub/linux/dotfile/ # "Dotfile
Generator.tcl"
http://asciipr0n.com/fp/ibpconf.sh-6.2.tar.gz # "ibpconf.bash"

For bash profiles in general, a fine place to look is:

http://www.dotfiles.com/files/3/

=Brian

0 new messages