How to get ~/foo from /home/user1/foo?

Posted by Claudius on Super User See other posts from Super User or by Claudius
Published on 2012-11-28T21:07:10Z Indexed on 2012/11/28 23:06 UTC
Read the original article Hit count: 264

Filed under:
|
|
|

The Bash prompt supports the \w escape sequence, documented as

\w     the current working directory, with $HOME abbreviated
       with a tilde (uses the value of the PROMPT_DIRTRIM variable)

Is there any way to get a similar abbreviation for an arbitrary string? That is, is there a general command that does something like the following, provided that HOME=/home/user1

/home/user1         ? ~
/home/user1/a/1     ? ~/a/1
/home/user2/b/2     ? ~user2/b/2
/root               ? ~root

Sure, I could try something ugly with sed, but that is unlikely to give me the result I want in any case. :-)

The movitation behind this is that I would like to keep the titles in the tabs of my terminals as short as possible, hence abbreviate working directories where possible.

© Super User or respective owner

Related posts about linux

Related posts about bash