Wednesday, May 1, 2024
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 7186  / 1 Year ago, wed, december 7, 2022, 9:50:00

I can't remember where, but I've already seen the bash prompt's current path shortened in an interesting way: every directory contained in the path (excepted the last one) is replaced by its first letter only. For instance: path/to/some/directory would be shortened to p/t/s/directory.



How can I reproduce that behavior ?


More From » command-line

 Answers
6

After playing with this for a while I got the answer you require:



Add this to your .bashrc file in your home directory, exit the terminal and renter it and you will get you prompt.



PS1='$(eval "sps")$ '
sps() {
echo "$PWD" | sed -r 's|/([^/]{,2})[^/]*|/1|g'
}


It uses the declared function sps() to evaluate the path every time the variable PS1 which is the prompt, is displayed



ie



/ho/de/De/Ap/Ti$ pwd
/home/deth/Desktop/Apps/Tivo
/ho/de/De/Ap/Ti$


Or...if you insist on the one letter



PS1='$(eval "sps")$ '
sps() {
echo "$PWD" | sed -r 's|/(.)[^/]*|/1|g'
}


Which displays:



/h/d/D/A/T$ pwd
/home/deth/Desktop/Apps/Tivo
/h/d/D/A/T$

[#32313] Friday, December 9, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reangi

Total Points: 213
Total Questions: 102
Total Answers: 114

Location: Namibia
Member since Wed, Jan 19, 2022
2 Years ago
;